use*_*142 0 html javascript css jquery
如何使用jQuery更改p标签文本颜色css属性?我不想改变所有的p标签,只是一个特定的标签....
如果没有为每个p标签添加id,这是否可行?
<div class = "custimage"><img src = "img/notch.jpg" alt = "notch"/><p>Notch</p></div>
<div class = "custimage"><img src = "img/peak.jpg" alt = "peak"/><p>Peak</p></div>
<div class = "custimage"><img src = "img/shawl.jpg" alt = "shawl"/><p>Shawl</p></div>
Run Code Online (Sandbox Code Playgroud)
编辑:
我想要它,以便当用户点击特定的custimage时,p标签文本颜色会发生变化.我试过了:
$('.custimage').click(function(e) {
var cssObj = {
'background-color' : '#fff'
}
$(this).css(cssObj);
$('this p').css({color: '#000'});
});
Run Code Online (Sandbox Code Playgroud)
这不起作用.使用$('.custimage p').css({color: '#000'});更改所有图像中文本的颜色......
您应该能够在单击的.custimagediv中更改p标签的颜色,如下所示:
$('.custimage').click(function(e) {
$(this).find('p').css({color: '#000'});
});
Run Code Online (Sandbox Code Playgroud)
该.find()函数遍历DOM树以查找与给定选择器匹配的任何标记.您可以.find()在http://api.jquery.com/find/上阅读有关该功能的更多信息.