【问题标题】:How can I refine this Javascript code so it IGNORES links from images如何优化此 Javascript 代码,使其忽略图像中的链接
【发布时间】:2013-12-16 19:33:38
【问题描述】:

这是现有问题的变体

{请注意 - 这个问题是 existing StackOverflow question 的反面/反面,too complex 在那里回答}

从一开始就

我想对some code from this challenge做一些改进:

// the new base url
var base = ' https://www.example.co.uk/gp/wine/order?ie=UTF8&asin=';
var links  = document.getElementsByTagName('a');

for(var i = 0;i < links.length;i++){
    // check each link for the 'asin' value
    var result = /asin=([\d\w]+)/.exec(links[i].getAttribute('href'));
    if(result){
        // make a new url using the 'base' and the 'asin' value
        links[i].setAttribute('href', base+result[1]);
    }
}

现在,而不是它作用于所有链接,我可以让它只查看来自文本的链接吗?

这是一个 HTML sn-p 来说明我的意思:

<a href="/shop/product?ie=UTF8&amp;asin=Z00FDLN878&amp;tab=UK_Default" target="_blank"><img width="125" height="125" border="0" src="http://ecx.images-amazon.com/images/I/01W9a7gwosL.jpg" alt="43453"></a>

这是一个图片链接 - 我不希望它对此采取行动。

【问题讨论】:

    标签: javascript greasemonkey greasekit fluid-mac-app-engine


    【解决方案1】:

    straight javascript / DOM 中,在循环中测试链接:

    var base        = 'https://www.example.co.uk/gp/wine/order?ie=UTF8&asin=';
    var linksNoImg  = document.querySelectorAll ("a[href*='asin']");
    
    for (var J = linksNoImg.length - 1;  J >= 0;  --J) {
        var imgLink = linksNoImg[J];
    
        //--- Make sure it's not an image link:
        if ( ! imgLink.querySelector ('img') ) {
            //--- Check each link for the 'asin' value
            var result  = /asin=([\d\w]+)/.exec (imgLink.getAttribute ('href') );
            if (result) {
                // make a new url using the 'base' and the 'asin' value
                imgLink.setAttribute ('href', base+result[1]);
            }
        }
    }
    


    jQuery 让您只预选正确的链接:

    //--- The new base URL
    var base        = ' https://www.example.co.uk/gp/wine/order?ie=UTF8&asin=';
    var linksNoImg  = $("a[href*='asin']:not(:has(img))");
    
    linksNoImg.each ( function () {
        // check each link for the 'asin' value
        var result = /asin=([\d\w]+)/.exec(links[i].getAttribute('href'));
        if(result){
            // make a new url using the 'base' and the 'asin' value
            links[i].setAttribute('href', base+result[1]);
        }
    } );
    


    但这需要您在 Greasekit 中加载 jQuery——这对于除了最简单的脚本之外的任何脚本都是非常值得的。

    【讨论】:

      【解决方案2】:
      if (links[i].querySelector('img')) {
          // Link has an <img>! Oh no!
      }
      

      要支持旧版浏览器,请改为调用getElementsByTagName()(并检查是否有空数组)。

      【讨论】:

      • 能否提供完整的代码,以便我进行测试? (就像 Brock Adams 在 stackoverflow.com/questions/20255051/… 所做的那样)
      • @Ted:你不明白什么?只需将其放入循环中即可跳过这些元素。
      • 我对如何继续有一个模糊的想法,但我不可避免地会放错逗号或句号并破坏事情。我是否将它(if (links[i].querySelector('img')) {)放在第 4 行并在末尾添加 }
      • 我应该把它插入到第 8 行吗? (对不起,如果这是一个愚蠢的问题)。一旦我确认它有效,我很乐意接受(“绿色勾号”)你的回答。
      • 啊,我想我终于明白了。如果链接有一个&lt;img&gt;,那么我希望它什么都不做,那么我可以让它继续 for 循环而不执行任何操作
      猜你喜欢
      • 2020-05-29
      • 1970-01-01
      • 1970-01-01
      • 2011-04-09
      • 2017-08-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多