[英]How to override something in jQuery?
The HTML: HTML:
<a href='#' id='myLink1' class='areaLink activeArea' onClick="myFunction('1')">Click Me 1</a>
<a href='#' id='myLink2' class='areaLink activeArea' onClick="myFunction('2')">Click Me 2</a>
CSS: CSS:
.activeArea { font-weight: bold; }
I have the following code runny on document ready: 我对文档准备好以下代码:
$(document).ready(function() {
$('a.areaLink').click(function() {
$('a.areaLink').removeClass('activeArea');
});
});
And this code inside the function that executes on the onClick event: 在onClick事件上执行的函数中的以下代码:
function myFunction(num) {
$('#myLink'+num).addClass('activeArea'); // !important
}
OK, so when the page initially loads, visually the links should look like this: 好的,因此当页面最初加载时,外观上的链接应如下所示:
Click Me 1 点击我1
Click Me 2 点击我2
When the user clicks on either link, that link should become bold like so: 当用户单击任一链接时,该链接应变为粗体,如下所示:
Click Me 1 点击我1
Click Me 2 点击我2
So how can I make this line within the function: 因此,如何在函数中创建此行:
$('#myLink'+num).addClass('activeArea');
OVERIDE the code running on document ready? 覆盖运行在文档上的代码准备好了吗?
Remove the inline onclick from your a
tags. 从你的onclick删除内联a
标签。 You also do not need to have a function to reference the clicked link. 您也不需要引用引用链接的功能。 Use $(this)
instead. 使用$(this)
代替。
$('.areaLink').click(function() {
$(this).addClass('activeArea').siblings().removeClass('activeArea');
});
Should work fine. 应该工作正常。 on document ready only loads once. 准备就绪的文档仅加载一次。
Do you want it to be bold the next time the page is loaded for the user? 您是否希望下次为用户加载页面时将其显示为粗体?
Also, I believe 而且,我相信
$(this).addClass('activeArea');
Will work as well as the code you have there... you don't need to pass in a number and use this naming convention. 它将和您那里的代码一样好用...您无需传递数字并使用此命名约定。
Change your html to this: 将您的html更改为此:
<a href='#' class='areaLink'>Click Me 1</a>
<a href='#' class='areaLink'>Click Me 2</a>
And this is all the JS you need: 这就是您需要的所有JS:
$(document).ready(function() {
$('a.areaLink').bind('click', function(e) {
e.preventDefault();
var $this = $(this);
if($this.hasClass('activeArea'))
return;
$this.siblings().removeClass('activeArea');
$this.addClass('activeArea');
});
});
You could simplify it all by doing the following: 您可以通过执行以下操作简化所有操作:
<a href='#' id='myLink1' class='areaLink'>Click Me 1</a>
<a href='#' id='myLink2' class='areaLink'>Click Me 2</a>
And in your script: 在您的脚本中:
$('a.areaLink').click(function() {
$(this).addClass('activeArea');
$(this).siblings('.areaLink').removeClass('activeArea');
});
I would recommend doing something like this: 我建议做这样的事情:
<a href='#' id='myLink1' class='areaLink activeArea clicker' onClick="myFunction('1')">Click Me 1</a>
<a href='#' id='myLink2' class='areaLink activeArea clicker' onClick="myFunction('2')">Click Me 2</a>
notice the clicker class i have added to both. 请注意,我同时添加了两个clicker类。
now use this code 现在使用此代码
$(".clicker").click(function(){
$(this).addClass("whatever_class_you_want_to_add");
});
This means you dont need to worry about the number, because it is already known by the target of $(this) 这意味着您不必担心该数字,因为$(this)的目标已经知道该数字。
Why don't you just remove the .removeClass()
from the jQuery click event? 为什么不从jQuery click事件中删除.removeClass()
?
function myFunction(num) {
$('a.areaLink').removeClass('activeArea');
$('#myLink' + num).addClass('activeArea'); // !important
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.