I have below Div tag, having 3 tables in it. How change back ground color for 1st table?
<div id="WebPartWPQ2" width="100%" HasPers="false" allowExport="false">
<table width="100%" border="0" cellSpacing="0" cellPadding="0">
<table width="100%" border="0" cellSpacing="0" cellPadding=开发者_如何学JAVA"0">
<table width="100%" border="0" cellSpacing="0" cellPadding="0">
<div>
$('div#WebPartWPQ2 table:first').css('backgroundColor', 'blue');
This uses a valid querySelectorAll
selector, then adds a class, which is usually more desirable than setting individual styles.
$('#WebPartWPQ2 > table:first-child').addClass( 'someClass' );
Another way to do it with a valid qsa
selector would be to select all tables, and use the slice()
[docs] method to get the first.
$('#WebPartWPQ2 > table').slice(0,1).addClass( 'someClass' );
Selectors like :first
are not recognized by qsa
, and in my opinion it is better to avoid them.
The selectors used above are:
the
id-selector
[docs]'#WebPartWPQ2
the
child-selector
[docs]>
the
element-selector
[docs]table
the
first-child-selector
[docs]:first-child
you should add a "-" between background and color.
$('div#WebPartWPQ2 table:first').css('background-color', 'blue');
You can use the following selector to find the first table in the div
. Then just apply your color to it:
jQuery("table:first", "#WebPartWPQ2").css('backgroundColor', myColor);
精彩评论