[英]Selecting <p> within a div using jQuery
I want to select the second <p>
tag and style it within a itemize
class div. 我想选择第二个<p>
标签,并在itemize
类div中设置其样式。 Here is the example HTML: 这是示例HTML:
<div class="itemize">
<p> Order Summery</p>
<div>
<p><strong>Packages:</strong> </p> <!-- i want to select this P tag-->
<p><strong>Date:</strong> </p>
<p><strong>Style:</strong> </p>
</div>
</div>
I want to select and style the first <p>
which is immediately after the second <div>
. 我想选择第二个<div>
之后的第一个<p>
并设置样式。 The second <p>
has no ID or class. 第二个<p>
没有ID或类。
How can I select it via jQuery? 如何通过jQuery选择它?
$('.itemize div p:first').html()
Check this link: http://jsfiddle.net/QJTYx/ 检查此链接: http : //jsfiddle.net/QJTYx/
If you want to add class to that p tag: 如果要将类添加到该p标签:
$('.itemize div p:first').addClass('selected');
You can do this way: 您可以这样做:
$('.itemize > div > p:eq(0)')
.itemize > div
goes till: .itemize > div
直到:
<div class="itemize">
<p> Order Summery</p>
</div>
And 和
.itemize > div > p:eq(0)
<div class="itemize">
<p> Order Summery</p>
<div>
<p><strong>Packages:</strong> </p>
</div>
</div>
The >
allows to target direct children whereas eq(index)
is used to get first p
that you want. >
允许定位直接子级,而eq(index)
用于获得所需的第一个p
。
var test = $('.itemize').find('div:first').find('p:first').html();
alert(test);
Try here: http://jsfiddle.net/arvind07/H8vwA/ 在这里尝试: http : //jsfiddle.net/arvind07/H8vwA/
$('.itemize>div>p:first').addClass('someClass');
这应该可以解决问题
$('.itemize div p').first().addClass('hello');
You can try this.. 你可以试试看
$(".itemize div p:first").text();
hope it will works.. 希望它能工作..
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.