javascript - 在新选项卡中打开 Squarespace 幻灯片画廊

标签 javascript jquery squarespace

我正在尝试在新选项卡中打开幻灯片上显示的单个图像,我已经使用 jQuery 成功完成了这一操作,但问题仍然存在于悬停在图像上的元数据中。单击元时,页面会将用户重定向到同一选项卡而不是新选项卡。

我尝试的是使用 $('.slide .meta') 抓取元元素,并使用 event.preventDefault() 阻止元元素被点击在click()中。点击已被识别,但它仍然允许页面加载我认为很奇怪的网址。因此,任何帮助将不胜感激!

这是适用于图像的 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

最佳答案

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

<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,我已将其删除。

关于javascript - 在新选项卡中打开 Squarespace 幻灯片画廊,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31371696/

相关文章:

html - 如何让带有自定义 CSS 的 Squarespace 页面在移动设备上正确显示?

javascript - 在关闭 Jasny Off Canvas 时调用函数

jquery - 响应式 Html - 图像上的垂直线

cdn - 将AWS CloudFront CDN放在Squarespace网站前面需要哪些设置?

ios - 您可以从 Squarespace 托管网站深度链接到应用程序吗?

jquery - 在选择器中使用 var

javascript - 如何在输入文本中使用jquery打印“(quot)而不是”?

javascript按钮选择onload

javascript - jquery 删除表格行

javascript - 将 Canvas 转换为 HTML5 中固定大小的图像