English 中文(简体)
jQuery:如何响应特定类的锚点标签的点击事件?
原标题:jQuery: how to respond to the click event of an anchor tag of a specific class?
  • 时间:2011-02-17 23:44:52
  •  标签:
  • jquery

我有一个表,它有一个包含链接的列。列中的每个单元格都包含两个链接:

  • view
  • play

当我单击播放链接时,我想显示一个警告框。我已将所有播放链接分配给同一个班级播放

我的剧本是这样的:

$(document).ready(function(){
  $( a.play ).click(function(){
      alert( I was clicked );
  });
});

我知道上面的脚本不起作用,因为有不止一个元素与表达式匹配。然而,我已经尝试了几乎所有的东西,包括使用$(this)来尝试访问点击的元素,但仍然不起作用。我该怎么做才能在点击a.play链接时弹出警报?

顺便说一句,如果我在FF控制台中键入$(a.play),元素就会被正确选择,所以我知道我的选择器是正确的。

[编辑]

更正了上面片段中的拼写错误。从我到目前为止得到的评论来看,上面的代码似乎应该可以工作。下面是一些进一步的细节。我试图让事情变得简单,以防止人们因可能的危险而分心,但似乎还需要进一步的信息,因为我在页面上使用的一些插件之间可能存在冲突(尽管没有报告错误/警告)。

因此:

我正在使用DataTables插件用于创建可排序的分页表。在表的其中一列中,单元格包含“查看”和“播放”链接。选择“播放”链路后,我希望显示一个模式表单,可以使用该表单从用户处收集信息。我使用的是jQuery UI插件,并且几乎是逐字逐句地使用代码的副本此处,(只是为了测试概念)。

所以,我想做的是:

  1. Click on the link: $( a.play ) selects the elements correctly
  2. Display a modal form when any one of the play links is selected
最佳回答

您错过了文档之后的结束

   // ----v-------------------was missing
$(document).ready(function(){
  $( a.play ).click(function( event ){
      event.preventDefault(); // <---------you may want this to stop the link
      alert( I was clicked ); //                            from being followed
      return false; // <---------or this if you want to prevent bubbling as well
  });
});

示例:http://jsfiddle.net/Cz44v/

问题回答

尝试:

    $(document.ready(function(){
  $( a .play ).click(function(){
      alert( I was clicked );
  });
});

这个行吗

演示

HTML

<a class="play" href="#1">Play1</a>
<a class="play" href="#2">Play2</a>
<a class="play" href="#3">Play3</a>
<a class="play" href="#4">Play4</a>​

Java脚本

$(document).ready(function() {

    $( a.play ).click(function() {
        alert( I was clicked   + $(this).attr("href"));
    });
});​




相关问题
getGridParam is not a function

The HTML: <a href="javascript:void(0)" id="m1">Get Selected id s</a> The Function: jQuery("#m1").click( function() { var s; s = jQuery("#list4").getGridParam( selarrrow )...

selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

jQuery cycle page with links

I am using the cycle plugin with pager functionality like this : $j( #homebox ) .cycle({ fx: fade , speed: fast , timeout: 9000, pager: #home-thumbs , ...

jquery ui dialog opens only once

I have a button that opens a dialog when clicked. The dialog displays a div that was hidden After I close the dialog by clicking the X icon, the dialog can t be opened again.

jConfirm with this existing code

I need help to use jConfirm with this existing code (php & Jquery & jAlert). function logout() { if (confirm("Do you really want to logout?")) window.location.href = "logout.php"; } ...

Wrap text after particular symbol with jQuery

What I m trying to do, is wrap text into div inside ll tag. It wouldn t be a problem, but I need to wrap text that appears particularly after "-" (minus) including "minus" itself. This is my html: &...

热门标签