某些链接的jQuery事件委派

时间:2016-06-29 19:29:48

标签: javascript jquery html

我无法将点击功能委派给某些链接。基本上,我试图触发一个简单的淡出所有内部链接,不会杀死灯箱功能。这是我的问题(实质上):

HTML

<body>
   <a href="lightbox-images"></a>
   <div id="fade">
      <p>some content</p>
      <a href="example.com"></a>
      <a href="example2.com"></a>
   </div>
</body>

的jQuery

$("#fade").on("click", "a", function () {

// get the href attribute
var newUrl = $(this).attr("href");

// veryfy if the new url exists or is a hash
if (!newUrl || newUrl[0] === "#") {
    // set that hash
    location.hash = newUrl;
    return;
}

// now, fadeout the html (whole page)
$("body").fadeOut(function () {
    // when the animation is complete, set the new location
    location = newUrl;
});

// prevent the default browser behavior.
return false;
});

我无法弄清楚为什么这不起作用。该功能不在任何链接上执行。我正在尝试将其委​​托给“淡化”包装ID中的所有链接。

当我改变

$("#fade").on("click", "a", function () {

$("document").on("click", "a", function () {
//specifying all links

淡入淡出效果很好,但它会破坏网站所需的灯箱功能。

是否有更好的方法来委派此活动的链接?或者可能不以不同方式包含灯箱链接?理想情况下,我会保留上面的代码,以便使用$("document")在所有链接上执行,并从函数中排除灯箱库,但我不知道如何做到这一点。

0 个答案:

没有答案