[英]Change 'P' tag text color within a div element
How do I change the p tag text color css attribute using jQuery? 如何使用jQuery更改p标签文本颜色css属性? I don't want to change all p tags, just a particular one....
我不想更改所有的p标签,只是一个特定的标签。
Is this possible without adding an id for every p tag? 如果不为每个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>
EDIT: 编辑:
I want it so that when the user clicks on a particular custimage, the p tag text color is changed. 我想要这样,以便当用户单击特定的客户图像时,p标签的文本颜色会更改。 I have tried:
我努力了:
$('.custimage').click(function(e) {
var cssObj = {
'background-color' : '#fff'
}
$(this).css(cssObj);
$('this p').css({color: '#000'});
});
This doesn't work. 这行不通。 Using
$('.custimage p').css({color: '#000'});
使用
$('.custimage p').css({color: '#000'});
changes the colour of the text in all the images... 更改所有图像中文本的颜色...
You should be able to change the color of the p tag inside the clicked .custimage
div like this: 您应该能够像这样在单击的
.custimage
div中更改p标签的颜色:
$('.custimage').click(function(e) {
$(this).find('p').css({color: '#000'});
});
The .find()
function traverses down the DOM tree to find any tag that matches the given selector. .find()
函数遍历DOM树以查找与给定选择器匹配的任何标记。 You can read more about the .find()
function at http://api.jquery.com/find/ 您可以在http://api.jquery.com/find/上了解有关
.find()
函数的更多信息。
$(".custImage p") would get you all p tag's within a div with that class. $(“。custImage p”)将使您在该类的div中获得所有p标签。 You could then do what you want.
然后,您可以做您想要的。 If you give me more information I'll give you a better selector.
如果您给我更多信息,我会为您提供更好的选择器。
To change a specific 'p' using text contents as a selector - 要使用文本内容作为选择器来更改特定的“ p”,
$("p:contains('Notch')").css("color","red");
to get the one inside the div with class .custimage
that was clicked - 将div中的类与
.custimage
一起单击-
$('.custimage').click(function(e) {$(this).find('p').css("color","red")});
$('p').each(function(index) {
//Lets say you want to target P number 2
if(index = "2") {
$(this).css('color','red');
}
});
or you could target the image or alt tag.. would that work for you? 或者您可以定位图片或alt标签。。这对您有用吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.