How can I get the width / color of a div border in javascript?

I would like to determine if a div has a border. If so, I would like to change the border color to gray. Here is my code but not working.

var ele = document.get...;
if(ele.style.borderColor)
{
   ele.style.borderColor='666666';
}

ele.style.borderColoralways null. By the way, I cannot use jQuery here. Can anybody help?

+3
source share
4 answers
var ele = document.getElementById('a'),
    style = window.getComputedStyle(ele, null),
    sides = ['top', 'right', 'bottom', 'left'],
    maxBorder = 0;

for (var i = 0, length = sides.length; i < length; i++) {
    maxBorder = Math.max(maxBorder, parseInt(style.getPropertyValue('border-' + sides[i] + '-width')));
}

if (maxBorder) {
    ele.style.borderColor = '#666666';
}

jsFiddle .

+4
source

You can simply set the frame color and not try to read any property.

If the item has a border there , the color setting will not have any effect.

+1
source

, , , "#" . , : ele.style.borderColor='#666666';

+1

, , . " "

Since you cannot use jquery here, I would look at all the properties of the css border to determine if it has a border like border-style, border-width and border-color.

0
source

All Articles