具有id的div内的多个标签的选择器

时间:2011-06-01 18:10:18

标签: jquery

在jQuery中,如何为#con中的标签h1,h2,h3和p构建选择器?

$('#con h1, #con h2, #con h3, #con p')类似,但不重复#con

2 个答案:

答案 0 :(得分:20)

您可以执行以下任何操作:

$("#con h1, #con h2, #con h3, #con p")   // your original

$("h1, h2, h3, p", $("#con"))            // pass jQuery object in as context

$("h1, h2, h3, p", "#con")               // pass selector in as context

$("#con").find("h1, h2, h3, p")          // do what jQuery ultimately does 
                                         // in the end when passing context
                                         // as jQuery or as string selector

<强> I've created a nice little jsFiddle that demos all of this.

答案 1 :(得分:5)

$('h1, h2, h3, ...', $('#con'))会奏效。这将上下文设置为#con并在上下文(#con)内搜索h1,h2等。