Javascript Jquery单击事件<;a>;标签没有正确发射

Javascript Jquery单击事件<;a>;标签没有正确发射,javascript,jquery,html,events,click,Javascript,Jquery,Html,Events,Click,我试图捕获带有类的标记上的jquery click事件,但它没有触发 HTML:(有10个街区) 由于某些原因,警报总是返回1。我认为您希望将单击回调绑定到链接。您可能还希望阻止默认行为,否则您的页面将重新加载 $(".popup").click(function(){ var id = $(this).parent().parent().attr('id')[5]; alert(id); });​ 此外,有效地使用类可以在这些调用失控之前切断大部分parent调用。比如说 &

我试图捕获带有类的标记上的jquery click事件,但它没有触发

HTML:(有10个街区)


由于某些原因,警报总是返回1。

我认为您希望将单击回调绑定到链接。您可能还希望阻止默认行为,否则您的页面将重新加载

$(".popup").click(function(){
    var id = $(this).parent().parent().attr('id')[5];
    alert(id);
});​
此外,有效地使用类可以在这些调用失控之前切断大部分
parent
调用。比如说

<!-- Make the head div belong to class "head" -->
<div id="block1" class="head">
  <div class="slideshow">
    <a class="popup" href="#imgform">

$(".popup").click(function(){
    // If you click the first link, you'll get "block1" in the alert.
    var id = $(this).parents(".head").attr('id');
    alert(id);
});​

$(“.popup”)。单击(函数(){
//如果单击第一个链接,您将在警报中看到“block1”。
var id=$(this.parents(“.head”).attr('id');
警报(id);
});​

在事件处理程序中,需要使用实际单击的事件

$('a.popup').click(function() {
  var $id = $(this).parent().parent().attr('id');
  ...
});
请注意,从jQuery 1.7开始,.live()方法已被弃用。使用.on()或.click()附加事件处理程序

试试这个

$('a.popup').on('click',function(){
  alert($(this).parent().parent().attr('id')); // to check whether it is firing....
});
使用:

如果您使用的是jQuery 1.7+,请使用:

    $(document).on('click', 'a.popup' ,function(){ 
        //do Stuff
    });
这可能对你有帮助

  • 从jQuery 1.7开始使用
  • 在AJAX调用中使用
    data
    参数,这可以确保正确转义查询参数。如果你仍然坚持手工操作,你可以使用
  • 在您的特定情况下,您可以缓存很多东西。尽管如此,在加载完成后,这可能不适用于动态添加的元素

  • 此外,使用
    data-*
    属性,您可以轻松删除
    id
    属性解析,并获得更清晰的标记

  • 使用
    类名
  • 添加
    数据块id
    属性
  • 您肯定应该使用类名来设置元素的样式,而不是ID

  • 我也遇到过类似的问题,我无法点击我试过的()比live()好

    查看此链接了解更多信息查看页面末尾的最后三个示例您可能会得到更好的解决方案。

    使用

    $(document).ready(function() {
        $('a.popup').on('click',function(){
            var id = $(this).parent().parent().attr('id');
            id = id.substring(5);
            alert(id);
       });
    });
    
    你可以用

    $(document).on("click", "$popup", function() {
    var id = $popup.parent().parent().attr('id');
            id = id.substring(5);
            alert(id);
            $.get('index.php?o='+id, function(data) {
                $popup.fancybox({
                    //do stuff
                });
    
    });
    


    live()
    已弃用。在上使用
    ,为什么要将单击事件绑定到文档而不是链接?@DaniP。是的,当我试图使用.onAlso时,这是一个打字错误,我想你不应该在变量名中使用$。这不是PHP。如果要将
    $
    用作jQuery选择器,则应仅将其用作变量名的开头。
    live
    不推荐使用。应该改为在
    上使用
    。这是第n次不推荐使用
    live
    ,您应该在
    上使用
    弹出窗口
    类锚没有
    id
    属性,那么这有什么作用呢?这是返回id它应该是我真的看不出我做错了什么你原来的
    var$popup=$('a.popup')
    是一个由两个元素组成的数组,当您在没有索引的情况下使用它时,它总是使用
    var id=$popup.parent().parent().attr('id')中的第一个元素。请尝试在此行中更改
    var id=this.parent().parent().attr('id')尝试此操作时出现错误:无法获取未定义或null的属性“5”reference@R4VANG3R-是的,我做了更改,当您单击第一个图像时,它应该会提醒
    1
    。我想您需要:var$id=$(this).parents(“div”).eq(1).attr(“id”)@AkhilSekharan-您可以通过有效地使用类和属性来避免它。我对你最后的回答作了评论。很抱歉连接速度慢。谢谢,这看起来很性感。问题是在我的css中,我为每个块设置了样式differently@R4VANG3R,您应该使用类名来设置样式,而不是ID,例如
    vs
    。如果你不想,那么第一个选择就是你想要的(尽管不推荐)。希望这能有所帮助
        $(document).on('click', 'a.popup' ,function(){ 
            //do Stuff
        });
    
    $(document).ready(function() {
        $('a.popup').each(function() {
            var $this = $(this);
            var id = $this.parent().parent().attr('id').substring(5);
            $this.click(function() {
                $.get('index.php', {
                    o: id
                }, function(data) {
                    $popup.fancybox({
                        //do stuff
                    });
                });
            });
        });
    });​
    
    <div class="block" data-block-id="1">
      <div class="slideshow">
        <a class="popup" href="#imgform">
          <img src="<?php echo 'core/controllers/foto.php?o=1'; ?>" title="1" width="271px" height="413px">
        </a>
      </div>
    </div>
    
    $(document).ready(function() {
        $('a.popup').each(function() {
            var $this = $(this);
            var id = $this.parent().parent().data("block-id");
            $this.click(function() {
                $.get('index.php', {
                    o: id
                }, function(data) {
                    $popup.fancybox({
                        //do stuff
                    });
                });
            });
        });
    });​
    
    $(document).ready(function() {
        $('a.popup').on('click',function(){
            var id = $(this).parent().parent().attr('id');
            id = id.substring(5);
            alert(id);
       });
    });
    
    $(document).on("click", "$popup", function() {
    var id = $popup.parent().parent().attr('id');
            id = id.substring(5);
            alert(id);
            $.get('index.php?o='+id, function(data) {
                $popup.fancybox({
                    //do stuff
                });
    
    });