2015-07-12 33 views
0

我试图打开在幻灯片上显示的单个图像,这是我通过使用jQuery成功完成的新选项卡,但问题仍然存在于悬停在图像上的元数据中。当元素被点击时,页面会将用户重定向到相同的选项卡而不是新的选项卡。在新标签中打开Squarespace幻灯片图库

我试过的是抓取元的元素与$('.slide .meta'),并通过click()中的event.preventDefault()防止元被点击。点击被识别,但它仍然允许网页加载我认为很奇怪的网址。所以任何帮助将不胜感激!

这里是jQuery的片段,对于图像的工作原理:

<script> 

    // Make sure the page is loaded 
    // before running the script 
    $(document).ready(function() { 

    // Grab the gallary container's link 
    $('.sqs-gallery-container a').click(function(event) { 

     // Get the link element 
     var href = $(this).attr('class'); 

     // Add the attribute '_blank' to open the 
     // generic link to that new tab 
     $(this).attr('target', '_blank'); 

     // Grab the link 
     var link = $(this).attr('href'); 

     // Check if the link isn't equal to '#' 
     if(link !== '#'){ 
      // Open the page 
      window.open(link); 
     } 

     // Prevent the gallary from opening the link 
     // and let us do our own thing! 
     event.preventDefault(); 
    }); 

    }); // end ready 

</script> 

,这里是一个从Chrome的控制台复制幻灯片库的代码片段:

https://gist.github.com/iwatakeshi/95fc070a668198566588

幻灯片的样品可以在这里找到:

https://help.squarespace.com/guides/using-the-slideshow-gallery-block

或实际画廊:

Squarespace Slideshow Gallery

回答

0

您是否尝试过移动event.preventDefault()到块的顶部?所以,你的代码将变成:

<script> 
$(document).ready(function() { 
    $('.sqs-gallery-container a').click(function(event) { 
     event.preventDefault(); // preventDefault first 
     var link = $(this).attr('href'); 

     if(link != '#'){ 
      window.open(link); 
     } 
    }); 
}); 
</script> 

您也有一个未使用的变量href我删除。

+0

感谢您的更正!是的,我已经尝试过,但仍然存在问题。 – iwatakeshi