能否建议我如何处理多个 anchor 标记以实现点击功能?
我的代码是这样的:
<a id="test1" href="#" >test1</a>
<a id="test2" href="#" >test2</a>
我的 jQuery 功能是:
$('a').click(function(event) {
alert('click');
});
jQuery 点击功能适用于所有 anchor 标记,但我想根据id 属性 区分jQuery 功能..
最佳答案
看这个上面的id,做一个if语句或者switch(推荐switch):
$('a').click(function(event) {
switch(this.id) {
case 'test1':
alert('link with an id of test1!');
event.preventDefault();
break;
case 'test2':
alert('link with an id of test2!');
event.preventDefault();
break;
default:
//Default behavior
}
});
关于javascript - 如何在 JQuery 中区分具有 id 的多个 anchor 标记上的 onclick 乐趣,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7580995/