2014-10-31 40 views
0

我使用jQuery 2.1.1和我有这样的代码:如何可以设置处理程序的所有链接与真正的href?

//Load and execute script via AJAX 
function loadScript(e) { 
     $.getScript($(e.target).attr("href")).error(function (error) { 
      $.notify("Request error"); 
     }); 
    return false; //Cancel opening link in the browser 
} 

//Set handlers to all <a> elements with href attribute and href!='#' 
Desktop.prototype.init = function() { 
    $("body").on("click", $("a[href][href!='#']"), {}, loadScript); 
}; 

当我做单击在一个地方的文件,我看到错误消息(404)。为什么?我应该在这种情况下写什么?我将通过点击<a>标签来动态加载和执行脚本。

回答

0

对于没有href值为#的锚点元素,您可以使用以下语法进行事件委派。

$('body').on('click', 'a:not([href="#"])', loadScript); 

Here's a working jsFiddle

相关问题