Calling a method for a class created in Javascript/jQuery
http://jsfiddle.net/3BqtV/ Javascript:
$(document).ready(function(){
$("#first").click(function(){
$("#secondHolder p").addClass('red');
});
$(".red").click(function(){
$(this).css("color","red");
});
})
HTML:
<p id= "first">Click me first to give Second the class "red"</p>
<div id= "secondHolder"><p>Click on first, then click on me, and I开发者_如何学编程 should be red.</p> </div>
Basically, when one element is clicked, I want to add a class to another element. And then, I call a method on the click function of the new class and have it change color to red. Sorry if it's a basic question, I'm kinda new to javascript. Thanks for the help!
Instead of using .click
, use .live('click'
- it matches any element with that selector now, or at any future point.
http://jsfiddle.net/3BqtV/1/
$(document).ready(function(){
$("#first").click(function(){
$("#secondHolder p").addClass('red');
});
$(".red").live('click', function(){
$(this).css("color","red");
});
})
精彩评论