How do I detect the inherited background-color of an element using jQuery/JS?

后端 未结 6 919
礼貌的吻别
礼貌的吻别 2020-11-30 11:40

Using jQuery (or just JavaScript), how do I detect the inherited background-color of an element?

For example:

6条回答
  •  南笙
    南笙 (楼主)
    2020-11-30 12:12

    This could be accomplished by using the parent() in a loop until you reach the body tag:

    I've set up a quick jsfiddle site with a little demo based on your code.

    Edit: Good catch fudgey. After doing some testing it appears that IE7 will return 'transparent' instead of the rgba(0,0,0,0) value. Here's an updated link which I tested in IE7, Chrome 7, and Firefox 3.6.1.2. Another caveat with this approach: Chrome/Firefox will return rgb(255,0,0); IE returned 'red'.

提交回复
热议问题