簡體   English   中英

如何不在側面導航和頂部導航欄中定位定位標記

[英]How to not target anchor tags in side navigation and top navigation bar

每當出現外部鏈接時,我都會彈出一個窗口。 如果不是外部鏈接,則該鏈接應在新選項卡中打開。 我遇到的問題是這種方法的目標是導航欄中的定位標記,並在新標簽頁中打開頁面。 僅應在pdf文檔中發生這種情況。

以下是我的邏輯,它檢測它是否是外部鏈接。 我很難忽略導航欄和側面導航欄中的鏈接。

$( document ).ready(function() {

$("a:not('.frontEndToolsModal')").on('click', function(e){
    e.preventDefault();
    var url = $(this).attr('href'),
        host = location.host;               

    if (url.indexOf(host) > -1 || url.indexOf('http','https') == -1){
        /* If we find the host name within the URL,
           OR if we do not find http or https, 
           meaning it is a relative internal link.

           The following statements is to not interefere with Mura CMS front end tools
        */

        if(url.indexOf('/admin/?muraAction=cArch.list') == 0){
            var newTab = window.open(url, '_blank');
            newTab.focus();
        }
        else if(url.indexOf('/admin/?muraAction') == 0){
            //do nothing
        }else{                          
            var newTab2 = window.open(url, '_blank');
            newTab2.focus();
        }
    }

對於導航欄,它們具有以下結構:

<ul id="navPrimary" class="nav navbar-nav nav-nowrap">

        <li class="first" id="navAboutUs"><a href="#">About Us</a></li>
</ul>

對於側面導航欄,它具有以下html:

<nav id="navStandard" class="mura-nav-standard well">
        <ul class="nav nav-list">

    <li class="first"><a href="#">Medical Services Authorization Status Tracker</a></li> 

我的問題是:如何在單擊功能中不包括側面導航欄和頂部導航欄?

我可以通過使用attr()並檢查錨標記是否包含“ target ='_ blank'”來弄清楚。

所以我做了以下事情:

if($(this).attr("target") != '_blank'){
    window.location.href = url;
}
else{
    var newTab1 = window.open(url, '_blank');
    newTab1.focus();
}

這段代碼:

$("a:not('.frontEndToolsModal')").on('click', function(e){
  // stuff happens
}

......是說:“任何在點擊a確實有類元素frontEndToolsModal ......事情發生。

這意味着您網站上除具有該類的鏈接外的所有鏈接。 對於您的點擊功能,選擇器可能太寬泛了。 真正的解決方案是找出一個不同的選擇器,該選擇器適用於單擊后您確實希望該功能運行的項目。 這意味着可能要將一些新類添加到要觸發函數並相應更新JS的那些項中:

$("a.someNewClass").on('click', function(e){
  // stuff happens
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM