【问题标题】:jQuery select a href with specific word insidejQuery选择一个带有特定单词的href
【发布时间】:2019-08-20 06:54:18
【问题描述】:

我尝试更改 jQuery 代码。我需要将代码行更改为某些内容,即选择所有包含我的网站网址的href链接,而不是链接中的“访问”一词。

以下代码是插件中的标准代码,我想更改它。

让我们看看网址。

https://www.example.com

jQuery select all a href 因为我的网站 url 在示例内部,但我有外部链接,不应选择。

外部链接https://www.example.com/visit/1234

我试过(添加 :not.a[href*="visit] 到 https 行

jQuery('a[href^="http://'+document.domain+'"],a[href^="https://'+document.domain+'"]:not.a[href*="visit],a[href^="/"],a[href^="?"]').each(function()

完整代码(123 代替插件名称)

<?php if (isset($_GET['123_123'])) { ?>
            function 123_add_link() {
                jQuery('a[href^="http://'+document.domain+'"],a[href^="https://'+document.domain+'"],a[href^="/"],a[href^="?"]').each(function() {
                    this.href += (/\?/.test(this.href) ? '&' : '?') + '123=true&123_123=true';
                });
            }
            jQuery( document ).ajaxComplete(123_add_link());
            jQuery(document).ready(123_add_link());
        <?php } ?>

【问题讨论】:

    标签: jquery


    【解决方案1】:

    您可以使用.not() 方法而不是:not pseudoselector 过滤所有匹配的选择器:

    $('a[href^="https://www.example.com"],a[href^="http://www.example.com"]')
        .not('[href*="/visit/"]')
        .css('background-color', 'red');
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script>
    <a href="https://www.example.com/1234">Internal</a>
    <a href="https://www.example.com/visit/123">External 1</a>
    <a href="http://www.example.com/visit/1234">External 2</a>
    <a href="http://www.example.com/1234">Internal</a>
    <a href="https://www.example2.com/visit/1234">Hotlink</a>

    【讨论】:

      猜你喜欢
      • 2018-08-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-29
      • 2019-10-16
      • 2021-09-18
      • 1970-01-01
      • 2012-01-25
      相关资源
      最近更新 更多