调用 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>
基本上,当单击一个元素时,我想向另一个元素添加一个类。然后,我调用新类的单击函数上的方法,并将其颜色更改为红色。 抱歉,如果这是一个基本问题,我对 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不要使用
.click
,而是使用.live('click'
- 它会立即或在将来的任何时刻与该选择器匹配任何元素。http://jsfiddle.net/3BqtV/1/
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/