1

技术

我正在使用 jQuery 1.9.1 和 twitter bootstrap 2.3.1

语境

我目前在 twitter bootstrap typeahead 插件中遇到一个错误,当自动完成下拉菜单打开时,它会阻止您在 input[type="text"] 中输入“&”字符(与号返回键码 38,向上箭头是键码 38)。

我希望能够查看附加到 input[type="text"] 的所有事件并找出导致问题的原因。

问题

// bootstrap typeahead plugin
// bootstrap.js:2126
this.$element
  .on('focus',    $.proxy(this.focus, this))
  .on('blur',     $.proxy(this.blur, this))
  .on('keypress', $.proxy(this.keypress, this))
  .on('keyup',    $.proxy(this.keyup, this))

// bootstrap.js:2171
move: function (e) {
  if (!this.shown) return

  switch(e.keyCode) {
    case 9: // tab
    case 13: // enter
    case 27: // escape
      e.preventDefault()
      break

    case 38: // up arrow
      e.preventDefault()
      this.prev()
      break

    case 40: // down arrow
      e.preventDefault()
      this.next()
      break
  }

  e.stopPropagation()
}

代理方法返回一个具有给定上下文的新函数,因此很难调试。

我在 Chrome 中发现的内容 - 对我没有帮助

// Get event listeners on element
getEventListeners($('[name="searchTerm"]')[0])

// Get the keydown event to see what's going on
getEventListeners($('[name="searchTerm"]')[0]).keydown[0].listener

// Returns
function (a){return typeof f!="undefined"&&(!a||f.event.triggered!==a.type)?f.event.dispatch.apply(i.elem,arguments):b}

// <function scope> points back to the function above
// ['<function scope>'] is an example
getEventListeners($('[name="searchTerm"]')[0]).keydown[0].listener['<function scope>']

我在 jQuery 中发现的东西 - 帮我尴尬?

// Get event listeners on element
// $element.data('events') is no longer supported. version >= 1.8
$._data($('[name="searchTerm"]')[0], 'events')

// Get the keydown event to see what's going on
$._data($('input.search-query.span2')[0], 'events').keydown[0].handler

// Returns
function (){return a.apply(c,f.concat(F.call(arguments)))}

// <function scope> points to the correct function
// ['<function scope>']['Closure']['a'] is an example
$._data($('input.search-query.span2')[0], 'events').keydown[0].handler['<function scope>']['Closure']['a']

// Returns
function (e) { this.suppressKeyPressRepeat = ~$.inArray(e.keyCode, [40,38,9,13,27]) this.move(e) }

问题

  1. 有没有办法从 javascript 函数中获取 <function scope> 和 <closure> 上下文?
  2. 有没有更简单的方法来调试使用 jQuery 在 DOM 元素上触发的 $.proxy 事件侦听器?
  3. 有没有更好的方法来调试这些事件?
4

1 回答 1

1

您可以尝试显示 jQuery 事件处理程序的jQuery Debugger Chrome 扩展。Firefox Nightly 也有类似的功能

于 2014-08-15T21:01:59.657 回答