在更新的jQuery库中从.live转换为.on
点击此链接: 我觉得我所拥有的是正确的,但它没有执行。我正在从jQuery在更新的jQuery库中从.live转换为.on,jquery,Jquery,点击此链接: 我觉得我所拥有的是正确的,但它没有执行。我正在从jQuery1.5升级到1.9,我已经用.on()替换了.live(),但是我的代码仍然没有正确执行。有什么我做错了吗 是: 现在: 完整代码: $(function(){ // Link handling. $('a[rel != "noAJAX"]').on('click', 'a', function){ if ($(this).attr('name') != ""){
1.5
升级到1.9
,我已经用.on()
替换了.live()
,但是我的代码仍然没有正确执行。有什么我做错了吗
是:
现在:
完整代码:
$(function(){
// Link handling.
$('a[rel != "noAJAX"]').on('click', 'a', function){
if ($(this).attr('name') != ""){
var currentDiv = $(this).attr('name');
}else{
var currentDiv = divID;
}
$(currentDiv).html(loadingHTML);
$(currentDiv).load($(this).attr('href'));
event.preventDefault();
});
$('form[rel != "noAJAX"]').on('submit', function(event){
if ($(this).attr('name') != ""){
var currentDiv = $(this).attr('name');
}else{
var currentDiv = divID;
}
$(currentDiv).html(loadingHTML);
$.post($(this).attr('action'), $(this).serialize(), function(html){
$(currentDiv).html(html);
});
return false;
});
});
您使用的.on()
不正确,并且您提前关闭了函数:
$('a[rel != "noAJAX"]').on('click', 'a', function){ ... }
即使将语法更正为以下内容,它也不会起作用:
$('a[rel != "noAJAX"]').on('click', 'a', function(){ ... });
上面的一行简单地指示jQuery侦听冒泡到锚元素的click事件,该锚元素具有“noAJAX”的rel
属性,并且该事件必须来自嵌套的锚元素。此外,属性选择器=代码>无效。您可能需要尝试查看:not()
。你应该试试:
$(document).on('click', 'a:not([rel="noAJAX"])', function() {...});
请参见此处的概念证明提琴:您是否按照我在对上一个问题的评论中的建议进行了操作?@MelanciaUK,但这不是“活的”。
$('a[rel != "noAJAX"]').on('click', 'a', function(){ ... });
$(document).on('click', 'a:not([rel="noAJAX"])', function() {...});