CSS Issue in JQuery [duplicate]

蓝咒 提交于 2019-12-24 10:03:27

问题


Possible Duplicate:
Why does $('#table > tr') selector not match? (always return 0)

I tried to apply color to rows of a table using the following code. It doesn't work, but I don't understand why. Could someone explain why or point me in the right direction?

HTML:

<table id="tblSample" border="1" cellpadding="0" cellspacing="0" width="300px">
    <tr>
        <td>1</td>
        <td>1</td>
    </tr>
    <tr>
        <td>2</td>
        <td>2</td>
    </tr>
</table>

JQuery:

$("#tblSample > tr").css("background-color", "gray");

回答1:


You're missing the elusive tbody element.

http://jsfiddle.net/m7HTt/

You can do this:

$("#tblSample > tbody > tr").css("background-color", "gray");

or this:

$("#tblSample tr").css("background-color", "gray");



回答2:


If you are looking to do alternating row colors you can do something as simple as this as well.

$("#tblSample tr:even").css("background-color", "gray");



回答3:


Try it

$("#tblSample  tr").css("background-color", "gray");

or

$("#tblSample tr td").css("background-color", "gray");


来源:https://stackoverflow.com/questions/5125407/css-issue-in-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!