本文介绍了如何在div中选择某些HTML元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在DIV中选择某些HTML元素.像这样:
I want to select certain HTML elements in a DIV. Something like this:
$("#foo table,tr,td,span,p" ).addClass( Myclass );
但是它将类也添加到DIV"#foo"之外.IE.只有div"#foo"中的元素才是要添加的类"Myclass".谢谢.
But it adds the class also outside of DIV "#foo".I.e. only the elements in div "#foo" are the class "Myclass" to add.Thanks.
推荐答案
问题:逗号,
分隔自主选择器.您必须先选择#foo
,然后选择内部元素table, tr, td, span, p
.
Problem: Comma ,
separates autonomous selectors. You have to select #foo
first, then select your inner elements table, tr, td, span, p
.
您可以使用 context 属性:
$("table, tr, td, span, p", "#foo").addClass("Myclass");
或者您可以链接选择器:
or you can chain your selectors:
$("#foo").find("table, tr, td, span, p").addClass("Myclass");
这篇关于如何在div中选择某些HTML元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!