点击即使不触发,即使我使用'm '

Click even not triggering even though I'm using "on"

本文关键字:      更新时间:2023-09-26

我有以下代码:

  if(amStatus === "Logout") {
    $('#row-qfauto-1 .large-9').append('<a id="next-password" class="button button-next" href="javascript:;">Next</a>')
    $('#next-password').click(function() {
      $('#row-html2-0').addClass('next-visible')
      $(window).resize()
    })
  }
  $('.button-next').on('click', function() {
    $(this).hide()
    $(window).resize()
  })

可以看到,button-next是用jQuery动态添加的。因此,为了使按钮在点击时消失,我决定使用on

然而,按钮并没有消失。它就呆在那里。当我做

  $('.button-next').click(function() {
    $(this).hide()
    $(window).resize()   
  })
在控制台中

两次,然后按钮消失。奇怪。有什么问题吗?

Live site: http://www.chineselearnonline.com/amember/signup/cloprogressive

试试这个:

if (amStatus === "Logout") {
  $('#row-qfauto-1 .large-9').append('<a id="next-password" class="button button-next" href="javascript:;">Next</a>')
  $('#next-password').click(function() {
    $('#row-html2-0').addClass('next-visible')
    $(window).resize()
  })
}
$('#row-qfauto-1 .large-9').on('click', '.button-next', function(e) {
  e.preventDefault();
  $(this).hide()
  $(window).resize()
});

注意:您将事件侦听器分配给#next-password以及.button-next,这是您想要实现的方式吗?

还要确保你的DOM中没有多余的id

尝试事件委派

$(document).on("click", ".button-next", function() {
  // do stuff
})

或在使用jQuery()

创建元素时附加click事件
   $("#row-qfauto-1 .large-9")
   .append($("<a></a>", {
       "id":"next-password",
       "class":"button button-next",
       "href":"javascript:;"
       "on": {
            "click": function() {
                       $(this).hide()
                       $(window).resize()
                     }
       },
       "html":"Next"  
    }))
相关文章:
  • 没有找到相关文章