简体   繁体   English

如何排除具有特定 href 的锚标记?

[英]How to exclude an anchor tag with a certain href?

I've a JS code that take the UTM and other url parameter so when the user navigate between pages I don't lose their track.我有一个采用 UTM 和其他 url 参数的 JS 代码,因此当用户在页面之间导航时,我不会失去他们的踪迹。 The code is working great when the href is a page but not when it's related to an id element当 href 是一个页面时,代码工作得很好,但当它与 id 元素相关时就不行了

I tried to exclude it in the if statement or to add the parameters after the id.我试图在 if 语句中排除它或在 id 后添加参数。

function getRefQueryParam(name) {
    name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]');
    var regex = new RegExp('[\\?&]' + name + '=([^&#]*)');
    var results = regex.exec(location.search);
    return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, ' '));
};

var utmParamQueryString = '',
    utmParamQueryStringTrimmed = '',
    utm_source = '',
    utm_medium = '',
    utm_content = '',
    utm_campaign = '',
    utm_term = '';

(function() {
    utm_source = getRefQueryParam("utm_source");
    utm_medium = getRefQueryParam("utm_medium");
    utm_content = getRefQueryParam("utm_content");
    utm_campaign = getRefQueryParam("utm_campaign");
    utm_term = getRefQueryParam("utm_term");
    gclid = getRefQueryParam("gclid");
    fbclid = getRefQueryParam("fbclid");

    if (utm_source) {
        utmParamQueryString += '&utm_source=' + utm_source;
    }
    if (utm_medium) {
        utmParamQueryString += '&utm_medium=' + utm_medium;
    }
    if (utm_content) {
        utmParamQueryString += '&utm_content=' + utm_content;
    }
    if (utm_campaign) {
        utmParamQueryString += '&utm_campaign=' + utm_campaign;
    }
    if (utm_term) {
        utmParamQueryString += '&utm_term=' + utm_term;
    }
    if (gclid) {
        utmParamQueryString += '&gclid=' + gclid;
    }
    if (fbclid) {
        utmParamQueryString += '&fbclid=' + fbclid;
    }

    if(utmParamQueryString.length > 0) {
        utmParamQueryString = utmParamQueryString.substring(1);
        utmParamQueryStringTrimmed = utmParamQueryString;
        utmParamQueryString = utmParamQueryString;
    }
    if (!utmParamQueryString) return;
    var navLinks = document.querySelectorAll('a');

    navLinks.forEach(function(item) {
      if (item.href.indexOf('/') === 0 || item.href.indexOf(location.host) !== -1 ) {
        if (item.href.indexOf('?') === -1) {
            item.href += '?';
        } else {
          item.href += '&';
        }
        item.href += utmParamQueryString;
      }
    });
})();


So it's searching every anchor tag and after the page uri the code insert the parameters that the current URL have.因此,它会搜索每个锚标记,并在页面 uri 之后插入当前 URL 具有的参数。 The issue is that it inserts code even what it's an id call <a href="#top"></a> and the call doesn't work anymore because the parameters isn't insert after but in between like so: utm_source=google&utm_medium=cpc&utm_campaign=test_cpc&utm_term=test #top &utm_source=google&utm_medium=cpc&utm_campaign=test_cpc&utm_term=test问题是它插入了代码,即使它是一个 id 调用<a href="#top"></a>并且该调用不再起作用,因为参数不是插入之后而是介于两者之间,如下所示: utm_source= google&utm_medium=cpc&utm_campaign=test_cpc&utm_term=test #top &utm_source=google&utm_medium=cpc&utm_campaign=test_cpc&utm_term=test

You can try use attribute selector on your querySelectorAll like this:您可以尝试在querySelectorAll上使用属性选择器,如下所示:

document.querySelectorAll('a:not([href^="#"])')

This will pick all a tags that the href attribute does not start with # .这将选择所有的a标签的href属性不下手#

You can, of course, try and modify it based on your needs.当然,您可以根据自己的需要尝试修改它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM