2014-10-17 50 views
0

。对下面这个链接:过渡到更新的jQuery库

jQuery 1.9 .live() is not a function

我觉得我有什么是正确的,但它不执行。我是从jQuery的1.5升级到1.9,我把它换成.live().on()但我的代码仍然未正确执行。有什么我做错了吗?

是:

$('a[rel != "noAJAX"]').live('click', function(event){ 

现在:

$('a[rel != "noAJAX"]').on('click', 'a', function){ 

全码:

$(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; 
    }); 
}); 
+0

你做什么,我在你的最后一个问题的意见建议? – j08691 2014-10-17 20:58:59

+0

@MelanciaUK这不是“活”虽然。 – user2864740 2014-10-17 21:01:53

回答

0

您使用的.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() {...}); 

看到,有证据的概念小提琴这里:http://jsfiddle.net/teddyrised/rh38eu4n/