开发者

Calling a method for a class created in Javascript/jQuery

开发者 https://www.devze.com 2023-04-04 08:40 出处:网络
http://jsfiddle.net/3BqtV/ Javascript: $(document).ready(function(){ $(\"#first\").click(function(){ $(\"#secondHolder p\").addClass(\'red\');

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");
            });
})
0

精彩评论

暂无评论...
验证码 换一张
取 消