How do I change the p tag text color css attribute using jQuery? I don't want to change all p tags, just a particular one....
Is this possible without adding an id for every p tag?
<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. 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'});
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').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. You can read more about the .find()
function at http://api.jquery.com/find/
$(".custImage p") would get you all p tag's within a div with that class. 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:contains('Notch')").css("color","red");
to get the one inside the div with class .custimage
that was clicked -
$('.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?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.