我以为我理解这一点,但显然不是。
http://jsfiddle.net/tmyie/w6nMe/2/
$('p:first-child').css({color:'red'});
不会更改任何文字的颜色。
<div class="post text"> <a href="#">this is a url</a>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ex optio velit culpa nisi nemo ab voluptatem vitae voluptate molestiae recusandae? Ipsa dolorum ut dignissimos ipsam doloribus itaque reprehenderit nostrum a.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ex optio velit culpa nisi nemo ab voluptatem vitae voluptate molestiae recusandae? Ipsa dolorum ut dignissimos ipsam doloribus itaque reprehenderit nostrum a.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ex optio velit culpa nisi nemo ab voluptatem vitae voluptate molestiae recusandae? Ipsa dolorum ut dignissimos ipsam doloribus itaque reprehenderit nostrum a.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ex optio velit culpa nisi nemo ab voluptatem vitae voluptate molestiae recusandae? Ipsa dolorum ut dignissimos ipsam doloribus itaque reprehenderit nostrum a.</p>
</div>
<div class="post text"> <a href="#">this is a url</a>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ex optio velit culpa nisi nemo ab voluptatem vitae voluptate molestiae recusandae? Ipsa dolorum ut dignissimos ipsam doloribus itaque reprehenderit nostrum a.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ex optio velit culpa nisi nemo ab voluptatem vitae voluptate molestiae recusandae? Ipsa dolorum ut dignissimos ipsam doloribus itaque reprehenderit nostrum a.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ex optio velit culpa nisi nemo ab voluptatem vitae voluptate molestiae recusandae? Ipsa dolorum ut dignissimos ipsam doloribus itaque reprehenderit nostrum a.</p>
</div>
答案 0 :(得分:2)
我认为你在寻找:
$('p:first').css({color:'red'});
更新:
在<p>
<div>
$('div p:first-of-type').css({color:'red'});
答案 1 :(得分:2)
应该是
$('p:first').css({color:'red'});
更改
p:first-child {
background-color: orange;
}
到
p:first-of-type {
background-color: orange;
}
答案 2 :(得分:0)
答案 3 :(得分:0)
我认为您正在尝试选择每个父帖子的第一个p标签子项,这是我的解决方案:
$('div.post p:first-child').css({color:'red'})
答案 4 :(得分:0)
尝试此代码:
$(function() {
$("p:first").addClass("first");
});
css:p.first { color: red; }
答案 5 :(得分:0)
在css中:first-child
选择器用于选择指定的选择器,只有它是其父节点的第一个子节点。
<强> fiddle 强>:
<强> CSS 强>:
p:first-child {
background-color: orange;
}
答案 6 :(得分:0)
这是一种黑客行为。您可以在jquery中包含以下内容。
$('p:nth-child(2)').css({color:'red'});