您好我正在尝试按照the following link的建议查看哪些事件被触发。但是,我没有看到警报..这里有什么问题?
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title> test ground</title>
<!-- JQuery specific -->
<script type="text/javascript" src="libs/jQuery/jQuery.min.js" ></script>
<script>
$(document).ready( function() {
$.each([
'blur', 'change', 'click', 'contextmenu', 'copy',
'cut', 'dblclick', 'error', 'focus',
'keydown', 'keypress', 'keyup',
'mousedown', 'mousemove', 'mouseout',
'mouseover', 'mouseup',
'mousewheel', 'paste',
'reset', 'resize',
'scroll', 'select',
'submit',
// W3C events
'DOMActivate', 'DOMAttrModified', 'DOMCharacterDataModified',
'DOMFocusIn', 'DOMFocusOut', 'DOMMouseScroll',
'DOMNodeInserted', 'DOMNodeRemoved', 'DOMSubtreeModified',
'textInput',
// Microsoft events
'activate',
'beforecopy',
'beforecut',
'beforepaste',
'deactivate',
'focusin',
'focusout',
'hashchange',
'mouseenter',
'mouseleave'
], function () {
$('a').live(this, function (evt) {
alert(this);
});
});
});
</script>
</head>
<body>
<a href="http://www.google.com"> test </a>
</body>
</html>
答案 0 :(得分:1)
我想这在回调上下文中被包装为一个对象,因此我想这个问题(在Firefox和Chrome最新版本中检查过。) 将回调更改为:
function (a, b) {
$('a').live(b, function (evt) {
alert(evt.type);
});
}
或
function () {
$('a').live(this.toString(), function (evt) {
alert(evt.type);
});
}
一旦我找出将其作为对象包装的原因,将使用更详细的描述更新帖子。
答案 1 :(得分:1)
我复制粘贴了堆栈溢出帖子中的原始代码,它对我来说非常适合。这是指向jsfiddle
的链接答案 2 :(得分:0)
第一次调用this
时,它是一个对象 - 将其设为字符串
$('a').live(this.toString(), function (evt) {
答案 3 :(得分:0)
试试这个:
<script>
$(document).ready( function() {
var el = $("#doIt");
$.each([
'blur', 'change', 'click', 'contextmenu', 'copy',
'cut', 'dblclick', 'error', 'focus',
'keydown', 'keypress', 'keyup',
'mousedown', 'mousemove', 'mouseout',
'mouseover', 'mouseup',
'mousewheel', 'paste',
'reset', 'resize',
'scroll', 'select',
'submit',
// W3C events
'DOMActivate', 'DOMAttrModified', 'DOMCharacterDataModified',
'DOMFocusIn', 'DOMFocusOut', 'DOMMouseScroll',
'DOMNodeInserted', 'DOMNodeRemoved', 'DOMSubtreeModified',
'textInput',
// Microsoft events
'activate',
'beforecopy',
'beforecut',
'beforepaste',
'deactivate',
'focusin',
'focusout',
'hashchange',
'mouseenter',
'mouseleave'
], function (index, value) {
el.on(value, function (evt) {
console.log("Whatever %o", this);
});
});
});
</script>
基本上确保只抓取一次元素(不需要调用jQuery加载),然后从.each()调用的函数中获取事件名称。