如何在 JavaScript 中获取 div 的边框宽度/颜色?
我想检测 div 是否有边框。如果是这样,我想将边框颜色更改为灰色。这是我的代码,但不起作用。
var ele = document.get...;
if(ele.style.borderColor)
{
ele.style.borderColor='666666';
}
ele.style.borderColor
始终为 null。 顺便说一句,我不能在这里使用 JQuery。 有人可以帮忙吗?
I'd like to detect if a div has border. If so, I'd like to change the border color to gray. Here's my code but does not work.
var ele = document.get...;
if(ele.style.borderColor)
{
ele.style.borderColor='666666';
}
The ele.style.borderColor
is always null.
BTW, I can't use JQuery here.
Could someone help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
jsFiddle。
jsFiddle.
您可以只设置边框颜色,而不尝试读取任何属性。
如果元素没有边框,则设置颜色不会产生任何效果。
You could just set the border color, and don't try to read any property.
If the element has no border, setting the color won't have any effect.
您所犯的错误是您没有在颜色十六进制代码之前指定“#”符号
所以您必须进行一些更改:
ele.style.borderColor='#666666';
The error you have made is that you haven't specified the '#' sign before the color hex code
So You will have to make a little change:
ele.style.borderColor='#666666';
我认为您得到的是空值,因为您缺少属性中间的连字符。 “border-color”
因为你不能在这里使用 jquery,所以我会查看所有 css 边框属性来确定它是否有边框,例如 border-style、border-width 和 border-color。
I think you're getting a null because you are missing the hyphen in the middle of the property. "border-color"
Since you can't use jquery here I would look at all the css border properties to determine if it has a border such as border-style, border-width and border-color.