如何使用jQuery获取元素的边框颜色值?

时间:2021-10-04 20:32:44

Using $("#id").css("background-color") to retrieve an element's background color (or most other CSS attributes) works just fine, but $("#id").css("border-color") returns an empty string.

使用$(“#id”)。css(“background-color”)来检索元素的背景颜色(或大多数其他CSS属性)工作正常,但$(“#id”)。css(“border-color” )返回一个空字符串。

How can I get the border color value used on the element?

如何获取元素上使用的边框颜色值?

2 个解决方案

#1


32  

CSS has "short-hand" properties that allows you to send multiple properties at once. Like font, border, background, etc. Well, the border-color CSS property actually sets the 4 properties border-top-color, border-right-color, border-bottom-color, and border-left-color.

CSS具有“短手”属性,允许您一次发送多个属性。像字体,边框,背景等一样。边框颜色CSS属性实际上设置了4个属性border-top-color,border-right-color,border-bottom-color和border-left-color。

If you want to get the border-color, you need to specify which side. For instance, to obtain the current value of border-left-color, you'd do:

如果要获取边框颜色,则需要指定哪一边。例如,要获得border-left-color的当前值,您需要:

$("#id").css("border-left-color")

This should work just fine since it seems as you're expecting that every side has the same color.

这应该工作得很好,因为你似乎期望每一面都有相同的颜色。

#2


8  

William was close... The property you're looking for is border-left-color, so in full you need

威廉很近......你正在寻找的属性是边框左边的颜色,所以你需要完整的

$('#ID').css("border-left-color")

and to set it

并设置它

$('#ID').css("border-left-color","blue");

for example.

例如。

Good luck, and hit me back in the comments.

祝你好运,并在评论中回击我。

#1


32  

CSS has "short-hand" properties that allows you to send multiple properties at once. Like font, border, background, etc. Well, the border-color CSS property actually sets the 4 properties border-top-color, border-right-color, border-bottom-color, and border-left-color.

CSS具有“短手”属性,允许您一次发送多个属性。像字体,边框,背景等一样。边框颜色CSS属性实际上设置了4个属性border-top-color,border-right-color,border-bottom-color和border-left-color。

If you want to get the border-color, you need to specify which side. For instance, to obtain the current value of border-left-color, you'd do:

如果要获取边框颜色,则需要指定哪一边。例如,要获得border-left-color的当前值,您需要:

$("#id").css("border-left-color")

This should work just fine since it seems as you're expecting that every side has the same color.

这应该工作得很好,因为你似乎期望每一面都有相同的颜色。

#2


8  

William was close... The property you're looking for is border-left-color, so in full you need

威廉很近......你正在寻找的属性是边框左边的颜色,所以你需要完整的

$('#ID').css("border-left-color")

and to set it

并设置它

$('#ID').css("border-left-color","blue");

for example.

例如。

Good luck, and hit me back in the comments.

祝你好运,并在评论中回击我。