admin管理员组文章数量:1410737
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...
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...
- 2 Which one do you want to change, and what constants can be used to find it (i.e. the paragraph next to a certain image, the last one, the 3rd one, the one with the text "Shawl" etc.)? – No Results Found Commented Oct 20, 2011 at 21:38
- It would be the one that the user has clicked on – user559142 Commented Oct 20, 2011 at 21:43
- OK yeah, that's pretty important - you should have included that in your original post - now you have 6 guess answers (3 deleted) because people jump all over the easy jquery questions. – No Results Found Commented Oct 20, 2011 at 21:47
4 Answers
Reset to default 6You 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./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.
$('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?
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")});
本文标签: javascriptChange 39P39 tag text color within a div elementStack Overflow
版权声明:本文标题:javascript - Change 'P' tag text color within a div element - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744843244a2628049.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论