简体   繁体   English

捕获数组中ID为ID的元素

[英]Capture clicks on elements with ID in array

I need to capture click on elements with ids stored in array ids . 我需要捕获点击存储在数组ids元素。

Eg: 例如:

ids = ['item1','item3','item4']

<a href='' id='item1'></a>
<a href='' id='item3'></a>
<a href='' id='item4'></a>

When clicking on 3 it should return 3 and prevent redirect. 点击3时,它应返回3并防止重定向。

If return 3 means alert 3... 如果返回3表示警报3 ......

$.each(ids, function(index, value) {
    $('#' + value).click(function(event) {
        alert(this.id);
        event.preventDefault();
    });
});​

Live DEMO 现场演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM