调用 Javascript/jQuery 创建的类的方法

发布于 2024-12-04 05:24:16 字数 718 浏览 2 评论 0原文

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>

基本上,当单击一个元素时,我想向另一个元素添加一个类。然后,我调用新类的单击函数上的方法,并将其颜色更改为红色。 抱歉,如果这是一个基本问题,我对 javascript 有点陌生。感谢您的帮助!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

山川志 2024-12-11 05:24:16

不要使用 .click,而是使用 .live('click' - 它会立即或在将来的任何时刻与该选择器匹配任何元素。

http://jsfiddle.net/3BqtV/1/

$(document).ready(function(){            
        $("#first").click(function(){
            $("#secondHolder p").addClass('red');
            });
        $(".red").live('click', function(){
            $(this).css("color","red");
            });
})

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");
            });
})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文