我有一个变量(div 元素),其中包含一些表格 html。
我可以使用此 JavaScript 向每个具有背景集的单元格添加一个类。
var tds = tempDiv.getElementsByTagName("TD");
for (var j = 0; j < tds.length; j++) {
var oTd = tds[j];
if (oTd.style.background.length > 0) {
oTd.className = 'faketh';
oTd.setAttribute('style', 'Clear');
} //if
}//for
我想做的是在 jquery 中做同样的事情。下面是我想出的,第二行工作正常,但第一行不行......
$(tempDiv).find("td[style*='background:']").addClass("faketh");
$(tempDiv).find("td").removeAttr('style');
如有任何帮助,我们将不胜感激。
编辑: 只是添加;我使用下面的代码没有问题。
$(tempDiv).find("tr:odd").addClass('row0');
$(tempDiv).find("tr:even").addClass("row1");
所以问题不是添加类...问题是我没有找到任何匹配的元素。这是 td 元素之一;
<td valign="top" class="faketd" style="border: 1pt solid windowtext; padding: 0cm 5.4pt; background: silver none repeat scroll 0% 0%; width: 131.4pt; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;">
<p style="margin: 0cm 0cm 0pt; text-align: justify;"><strong>VPN Name/Description:</strong></p>
</td>
最佳答案
我不认为你可以使用 jQuery 中的选择器来做到这一点。浏览器不会将样式属性存储为字符串,它是一个对象。
但是它已经实现了:http://code.google.com/p/aost/wiki/CustomJQuerySelectorInTellurium#:styles
或者你可以使用 $.each 类似 this :
$("img").each(function() {
if($(this).css('background').length > 0) {
$(this).addClass('faketh');
}
});
或者您可以使用 jQuery 过滤器:
var x = $("#tempDiv td").filter(function(i){
return $(this).css("background").length > 0;
});
关于javascript - Jquery 属性选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1374075/