Jquery |在新标签页中打开子文件夹的所有链接

Jquery | Open all links of subfolder in new tab

我需要在新选项卡中打开名为 documents 的 Wordpress 子文件夹的所有 url。例如“http://www.example.com/documents/*”

场景:显示目录 "documents" 中所有 post 的 post 网格时,每个 post 都会在新选项卡中打开。

我已经尝试编辑外部 link 脚本来为我工作,但我没有任何进展。我需要使用 JQuery /Javascript 甚至 WP 函数来完成此操作。

求助:)

您可以使用 Attribute Contains Selector 为包含 documents 文件夹的所有锚点附加事件处理程序。 使用 window.open(url, '_blank') 您将在新选项卡中打开 link:

// using jQuery
$('a[href*="documents/"]').on('click', function(e) {
    e.preventDefault();
    var win = window.open(this.href, '_blank');
    win.focus();
})

// using javaScript
 document.querySelectorAll('a[href*="documents/"]').forEach(function(ele, inex) {
    ele.addEventListener('click', function() {
        var e = event || window.event;
        e.preventDefault();
        var win = window.open(this.href, '_blank');
        win.focus();
    })
})

感谢@gaetanoM 帮我解决了这个问题!

对于有类似问题的人,最终的解决方案是:

    // Open all document post types in a new tab
    jQuery(document).ready(function($) {
    $(document).on('click', 'a[href*="documents/"]', function(e) {
    e.preventDefault();
    var win = window.open(this.href, '_blank');
    win.focus();
    })

    });