jquery - 属性的索引值

标签 jquery fancybox

我正在使用以下代码 ... ...

for($i=0; $i<90; $i++){
?>
 <a id='read[<?php print $i; ?>]' href="<?php print $textToshow; ?>"> Text Shown</a> 
<?php } ?> 

我想知道当用户点击 a href 时它的 id。类似 read[1] read[2] 等

最佳答案

$('a').click(function( e ) {
   alert(this.id);
   // e.preventDefault(); // Uncomment this line if you don't want
});                       //    to follow the link's href.

这分配了 click事件全部<a>单击时将提醒其 ID 的元素。

取消注释 e.preventDefault() 行以防止链接的默认行为(遵循其 href )。

最好向链接添加一个类属性,然后选择使用它:

$('a.someClass').click(function( e ) {
   alert(this.id);
   // e.preventDefault(); // Uncomment this line if you don't want
});  

这选择<a>具有类 "someClass" 的元素使用the class selector .

关于jquery - 属性的索引值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4221269/

相关文章:

jquery - 鼠标悬停背景移动

jquery - 具有 2 种图像尺寸(横向和纵向)的幻灯片

jquery - 更改 fancybox 设计 - 添加另一个不晃动窗口的 div

javascript - Fancybox 2 自定义标题和字幕 [来自 A(title) 和 IMG(alt)

javascript - jQuery 与 fancybox 相关的问题

javascript - 如何使用 jQuery 获取 DOM 实时集合?

javascript - 如何知道一个元素的类中是否有另一个元素

javascript - 在 Vue.js 中的对象属性之间切换

jquery - 如何根据 fancybox 内容高度(大覆盖或滚动页面)固定覆盖背景高度。

php - 在 fancybox 中预览 php/jquery 表单,然后提交或返回表单