【问题标题】:jquery .live function throwing error "Object [object Object] has no method 'live'" [closed]jquery .live函数抛出错误“对象[对象对象]没有方法'live'”[关闭]
【发布时间】:2013-03-28 22:27:03
【问题描述】:

我试图将click 事件添加到我项目中的 div Id 中,但它引发了错误。我不知道为什么。有人可以看看代码吗?

HTML:

<div id="container">
    <div id="header1">
        <input type="button" value="Search and Highlight!!" style="position: absolute; right: 100px; top: 10px;" />
    </div>
...

jQuery:

  var searchAttachPoint = document.querySelector('.header1');
  $('#header1').live("click", function () {
      myNameSpace.searchPrompt("Key the text and press Ok", false)
  }); // The error is thrown in this line
  var attachpoint = document.querySelector('.buttonAttachPoint');
  $(document).on('load', myNameSpace.LoadAllBooks("ajax/metadata.json", this.attachpoint));

【问题讨论】:

  • 您使用的是什么版本的 Jquery? live 在 Jquery 1.9 中被删除,api.jquery.com/live
  • 那些querySelector是干什么用的?
  • @Bergi 我以前从未见过这些......在快速谷歌之后,它似乎是 jQuery/sizzle 用来选择元素的“本机”JS 方法:developer.mozilla.org/en-US/docs/DOM/Document.querySelector
  • @jammypeach:我知道他们在做什么,我在问 OP 他试图用它们做什么(因为他似乎做错了)。
  • @Bergi 原谅我的假设 :)

标签: javascript html jquery


【解决方案1】:

.live() 已折旧,可替换为.on()

所以,

更改:$('#header1').live('click',function(){...})

收件人:$('body').on('click','#header1',function(){...})

body 应替换为最近的 #header1 静态父级

【讨论】:

  • 您甚至可以使用$("#container") 而不是$("body") 来帮助缩小目标范围。这取决于#container 是否是静态的
  • 理想情况下是#header1的父级
  • 好点,我会把它添加到我的答案中
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-04-19
  • 1970-01-01
  • 1970-01-01
  • 2014-05-12
  • 2012-04-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多