2015-05-06 167 views
0

我有一個腳本,如果頁面標題與<a>標籤的文本值相匹配,它將爲元素添加一個類。我的腳本中的else語句用於說明何時頁面標題與導航項目不匹配,並且活動鏈接被分配給Home菜單項(即導航欄中的第一個項目)。jQuery if和else語句都執行

我有一個菜單,就像這樣:

<ul class='menu'> 
    <li> <a href="#">Home</a> </li> 
    <!-- Page title - My Cool Website --> 
    <li> <a href="#">About Us</a> </li> 
    <!-- Page title - About Us --> 
    // etc.... 
</ul> 

這裏的腳本:

$(document).ready(function() { 
    var title = $(document).attr("title"); 
    $(".menu li a").each(function (e) { 
     var a = $(this).text(); 
     var exists = title.match(a); 
     if (exists) { 
      $(this).addClass("active"); 
     } else { 
      $(".menu li a").first().addClass("active"); 
     } 
    }); 
}); 

if和else語句似乎執行這樣第一和匹配的元素現在有一個active類添加到他們。

我試過在我的聲明結尾處使用stopPropagation();return false,但沒有成功。

任何人都可以告訴我爲什麼兩個正在執行?我假設我沒有優雅地跳過DOM,但JavaScript有點生疏。

+0

您可以創建問題 – Tushar

+0

@Tushar腳本被設計爲可以在多個頁面上工作,使用''標籤,所以我無法提供一個可用的jsFiddle。 – <span class="text-secondary"> <small> <a rel="noopener" target="_blank" href="https://stackoverflow.com/users/1474682/">WebDevDanno</a></span> <span></span> </small> </span> </p> </div> </div> </div> </div> </div> </article> </div> <div class="answer-title"> <span class="text-logo margin-top-sm">A</span> <h2 class="title h4">回答</h2> </div> <div class="item-description text-md markdown-body margin-bottom-40 voidso"> <article class="board-top-1 padding-top-10"> <div class="post-col vote-info"> <span class="count">3<i class="fa fa-thumbs-up"></i></span> <i class="fa fa-check fa-2x"></i> </div> <div class="post-offset"> <div class="answer fmt"> <p>您的問題背後的原因是,你必須多發錨<code class="prettyprint-override"><a></code> S,一些或其他能滿足一個<code class="prettyprint-override">if()</code>或<code class="prettyprint-override">else</code>如果添加循環內這些條件。</p> <p><code class="prettyprint-override">exists</code>應位於每個循環之外。在元素遍歷之後,即在循環之外,您需要檢查元素是否與<code class="prettyprint-override">title</code>匹配。</p> <pre><code class="prettyprint-override">$(document).ready(function() { var exists = false; var title = $(document).attr("title"); $(".menu li a").each(function(e) { var a = $(this).text(); exists = title.match(a); if(exists) { $(this).addClass("active"); return false; //break if statement matches. } }); if(!exists){ $(".menu li a").first().addClass("active"); } }); </code></pre> </div> <div class="post-info"> <div class="post-meta row"> <p class="text-secondary col-lg-6"> <span class="source"> <a rel="noopener" target="_blank" href="https://stackoverflow.com/q/30072771">來源</a> </span> </p> <p class="text-secondary col-lg-6"> <span class="float-right date"> <span>2015-05-06 09:29:54</span> </p> <p class="col-12"></p> <p class="col-12"></p></div> </div> <!-- comments --> <div class="comments"> <div itemprop="comment" class="post-comment"> <div class="row"> <div class="col-lg-1"><span class="text-secondary">+0</span></div> <div class="col-lg-11"> <p class="commenttext">感謝這位朋友,你已經幫助我思考如何優雅地退出if語句條件:)正確答案! – <span class="text-secondary"> <small> <a rel="noopener" target="_blank" href="https://stackoverflow.com/users/1474682/">WebDevDanno</a></span> <span></span> </small> </span> </p> </div> </div> </div> </div> </div> </article> <div> <script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-6208739752673518" data-ad-slot="1038284119" data-ad-format="auto" data-full-width-responsive="true"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </div> <article class="board-top-1 padding-top-10"> <div class="post-col vote-info"> <span class="count">7<i class="fa fa-thumbs-up"></i></span> </div> <div class="post-offset"> <div class="answer fmt"> <p>的問題是你的每個循環,假設你發現了第二個是在比賽,但仍然是每個循環繼續,因此第一個元素會再次獲得類</p> <pre><code class="prettyprint-override">$(document).ready(function() { var title = $(document).attr("title"); var exists; $(".menu li a").each(function (e) { var a = $(this).text(); exists = title.match(a); if (exists) { $(this).addClass("active"); return false; } }); if (!exists) { $(".menu li a").first().addClass("active"); } }); </code></pre> </div> <div class="post-info"> <div class="post-meta row"> <p class="text-secondary col-lg-6"> <span class="source"> <a rel="noopener" target="_blank" href="https://stackoverflow.com/q/30072740">來源</a> </span> </p> <p class="text-secondary col-lg-6"> <span class="float-right date"> <span>2015-05-06 09:28:23</span> </p> <p class="col-12"></p> <p class="col-12"></p></div> </div> </div> </article> <article class="board-top-1 padding-top-10"> <div class="post-col vote-info"> <span class="count">2<i class="fa fa-thumbs-up"></i></span> </div> <div class="post-offset"> <div class="answer fmt"> <p>你的「存在」爲每個變量重新初始化項目。那就是問題所在。使用此代碼來代替:</p> <pre><code class="prettyprint-override">$(document).ready(function() { var title = $(document).attr("title"); var exists = false; $(".menu li a").each(function (e) { var a = $(this).text(); if(title.match(a)) { exists = true; return false; } }); if (exists) { $(this).addClass("active"); } else { $(".menu li a").first().addClass("active"); } }); </code></pre> </div> <div class="post-info"> <div class="post-meta row"> <p class="text-secondary col-lg-6"> <span class="source"> <a rel="noopener" target="_blank" href="https://stackoverflow.com/q/30072765">來源</a> </span> </p> <p class="text-secondary col-lg-6"> <span class="float-right date"> <span>2015-05-06 09:29:40</span> </p> <p class="col-12"></p> <p class="col-12"></p></div> </div> </div> </article> <article class="board-top-1 padding-top-10"> <div class="post-col vote-info"> <span class="count">0<i class="fa fa-thumbs-up"></i></span> </div> <div class="post-offset"> <div class="answer fmt"> <p>試試這個:</p> <p></p> <div class="snippet" data-lang="js" data-hide="false"> <div class="snippet-code" id="sn1"> <pre class="snippet-code-js lang-js prettyprint-override"><code class="prettyprint-override">$(document).ready(function() { var title = $(document).attr("title"); $(".menu li a").each(function(e) { var a = $(this).text(); var exists = title.match(a); if(exists) { $(this).addClass("active"); } }); $(".menu li:first-child a").addClass("active") });</code></pre> </div> </div> <p></p> </div> <div class="post-info"> <div class="post-meta row"> <p class="text-secondary col-lg-6"> <span class="source"> <a rel="noopener" target="_blank" href="https://stackoverflow.com/q/30072788">來源</a> </span> </p> <p class="text-secondary col-lg-6"> <span class="float-right date"> <span>2015-05-06 09:30:29</span> </p> <p class="col-12"></p> <p class="col-12"></p></div> </div> </div> </article> <div> <script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-6208739752673518" data-ad-slot="1038284119" data-ad-format="auto" data-full-width-responsive="true"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </div> </div> <div class="clearfix"> </div> <div class="relative-box"> <div class="relative">相關問題</div> <ul class="relative_list"> <li> 1. <a href="http://hk.uwenku.com/question/p-ojvrhbqp-bme.html" target="_blank" title="if和else語句都會執行"> if和else語句都會執行 </a> </li> <li> 2. <a href="http://hk.uwenku.com/question/p-prrwohqb-ma.html" target="_blank" title="Javascript/jQuery,if和else語句都正在執行"> Javascript/jQuery,if和else語句都正在執行 </a> </li> <li> 3. <a href="http://hk.uwenku.com/question/p-orlfmnxh-bdw.html" target="_blank" title="if else語句執行"> if else語句執行 </a> </li> <li> 4. <a href="http://hk.uwenku.com/question/p-exmcqkbj-kz.html" target="_blank" title="nodejs - IF和ELSE都執行"> nodejs - IF和ELSE都執行 </a> </li> <li> 5. <a href="http://hk.uwenku.com/question/p-ycjandnj-bdw.html" target="_blank" title="Rails執行if和else語句"> Rails執行if和else語句 </a> </li> <li> 6. <a href="http://hk.uwenku.com/question/p-pbbsjxnm-dv.html" target="_blank" title="if和else語句在Stata中執行"> if和else語句在Stata中執行 </a> </li> <li> 7. <a href="http://hk.uwenku.com/question/p-egmtrgcl-beg.html" target="_blank" title="Javascript - 執行if和else語句"> Javascript - 執行if和else語句 </a> </li> <li> 8. <a href="http://hk.uwenku.com/question/p-smttarod-bez.html" target="_blank" title="兩個if和else語句執行"> 兩個if和else語句執行 </a> </li> <li> 9. <a href="http://hk.uwenku.com/question/p-ufahufyo-dw.html" target="_blank" title="if和else語句都在while循環中執行"> if和else語句都在while循環中執行 </a> </li> <li> 10. <a href="http://hk.uwenku.com/question/p-qubrzmnr-eu.html" target="_blank" title="jquery if else語句"> jquery if else語句 </a> </li> <div> <script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <ins class="adsbygoogle" style="display:block; text-align:center;" data-ad-layout="in-article" data-ad-format="fluid" data-ad-client="ca-pub-6208739752673518" data-ad-slot="4606349252"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </div> <li> 11. <a href="http://hk.uwenku.com/question/p-gauxakhg-bac.html" target="_blank" title="jQuery if/else語句"> jQuery if/else語句 </a> </li> <li> 12. <a href="http://hk.uwenku.com/question/p-muzvzriq-zm.html" target="_blank" title="jquery if else語句"> jquery if else語句 </a> </li> <li> 13. <a href="http://hk.uwenku.com/question/p-dziaatet-ob.html" target="_blank" title="JQuery Else if語句?"> JQuery Else if語句? </a> </li> <li> 14. <a href="http://hk.uwenku.com/question/p-ouuolqcy-ym.html" target="_blank" title="Else子句(在if語句)不執行"> Else子句(在if語句)不執行 </a> </li> <li> 15. <a href="http://hk.uwenku.com/question/p-qbanucpv-bno.html" target="_blank" title="if和else語句"> if和else語句 </a> </li> <li> 16. <a href="http://hk.uwenku.com/question/p-mmcrmpsp-md.html" target="_blank" title="PHP validate同時執行IF語句和ELSE語句"> PHP validate同時執行IF語句和ELSE語句 </a> </li> <li> 17. <a href="http://hk.uwenku.com/question/p-uydbrilf-zd.html" target="_blank" title="IF語句不執行,但ELSE IF語句正常工作"> IF語句不執行,但ELSE IF語句正常工作 </a> </li> <li> 18. <a href="http://hk.uwenku.com/question/p-hpocahis-bms.html" target="_blank" title="這兩個分支都在我的if-else語句中執行"> 這兩個分支都在我的if-else語句中執行 </a> </li> <li> 19. <a href="http://hk.uwenku.com/question/p-eczvnbnh-ne.html" target="_blank" title="if-else語句沒有執行?"> if-else語句沒有執行? </a> </li> <li> 20. <a href="http://hk.uwenku.com/question/p-mtoidnlv-bgy.html" target="_blank" title="if else語句沒有執行正確"> if else語句沒有執行正確 </a> </li> <li> 21. <a href="http://hk.uwenku.com/question/p-zspgvbhm-za.html" target="_blank" title="SQL IF ELSE語句不執行"> SQL IF ELSE語句不執行 </a> </li> <li> 22. <a href="http://hk.uwenku.com/question/p-eluhdtaf-bem.html" target="_blank" title="CodeIgniter if ... else語句未正確執行"> CodeIgniter if ... else語句未正確執行 </a> </li> <li> 23. <a href="http://hk.uwenku.com/question/p-gztpyjhe-hq.html" target="_blank" title="if-else語句在Java中不執行"> if-else語句在Java中不執行 </a> </li> <li> 24. <a href="http://hk.uwenku.com/question/p-dqxxwiux-uu.html" target="_blank" title="一行if..else if .. else語句"> 一行if..else if .. else語句 </a> </li> <li> 25. <a href="http://hk.uwenku.com/question/p-grxtroha-ee.html" target="_blank" title="shortHand if else-if和else語句"> shortHand if else-if和else語句 </a> </li> <li> 26. <a href="http://hk.uwenku.com/question/p-nqofzmvs-baq.html" target="_blank" title="if else if else語句(JS)"> if else if else語句(JS) </a> </li> <li> 27. <a href="http://hk.uwenku.com/question/p-fenjlumf-nr.html" target="_blank" title="jQuery Window Width else if語句"> jQuery Window Width else if語句 </a> </li> <li> 28. <a href="http://hk.uwenku.com/question/p-cropbyfu-bgv.html" target="_blank" title="jQuery if/else語句CSS"> jQuery if/else語句CSS </a> </li> <li> 29. <a href="http://hk.uwenku.com/question/p-bbtqeqga-bkp.html" target="_blank" title="if jquery中的else語句"> if jquery中的else語句 </a> </li> <li> 30. <a href="http://hk.uwenku.com/question/p-vrnlmkht-bkt.html" target="_blank" title="jquery .hover()與else if語句"> jquery .hover()與else if語句 </a> </li> </ul> </div> <div> <script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <ins class="adsbygoogle" style="display:block" data-ad-format="autorelaxed" data-ad-client="ca-pub-6208739752673518" data-ad-slot="1575177025"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </div> <div class="padding-top-10"></div> </div> </div> <script type="text/javascript" src="http://img.uwenku.com/uwenku/script/side.js?t=1644592048261"></script> <script type="text/javascript" src="http://img.uwenku.com/uwenku/plugin/highlight/highlight.pack.js"></script> <link href="http://img.uwenku.com/uwenku/plugin/highlight/styles/docco.css" media="screen" rel="stylesheet" type="text/css" /> <script type="text/javascript"> $('pre').each(function(i, e) { hljs.highlightBlock(e, "<span class='indent'> </span>", false) }); </script> <div class="col-lg-3 col-md-4 col-sm-5"> <div id="rightTop"> <div class="row"> <script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-6208739752673518" data-ad-slot="5415218910" data-ad-format="auto" data-full-width-responsive="true"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </div> <div class="row sidebar panel panel-default"> <div class="panel-heading font-bold"> 最新問題 </div> <div class="m-b-sm m-t-sm clearfix"> <ul class="side_article_list"> <li class="side_article_list_item"> 1. <a href="http://hk.uwenku.com/question/p-csmlyttv-tb.html" target="_blank" title="在角度工人中導入模塊?"> 在角度工人中導入模塊? </a> </li> <li class="side_article_list_item"> 2. <a href="http://hk.uwenku.com/question/p-qrtnwcvg-tn.html" target="_blank" title="Python布爾方法命名約定"> Python布爾方法命名約定 </a> </li> <li class="side_article_list_item"> 3. <a href="http://hk.uwenku.com/question/p-qvpcimgh-py.html" target="_blank" title="jquery background-image"> jquery background-image </a> </li> <li class="side_article_list_item"> 4. <a href="http://hk.uwenku.com/question/p-eyghaugq-qh.html" target="_blank" title="從s3檢索音頻時出錯"> 從s3檢索音頻時出錯 </a> </li> <li class="side_article_list_item"> 5. <a href="http://hk.uwenku.com/question/p-szhcyset-qt.html" target="_blank" title="如何將QR碼自動添加到圖像"> 如何將QR碼自動添加到圖像 </a> </li> <li class="side_article_list_item"> 6. <a href="http://hk.uwenku.com/question/p-oscplsex-rc.html" target="_blank" title="16個任務(1048.5 MB)的序列化結果的總大小大於spark.driver.maxResultSize(1024.0 MB)"> 16個任務(1048.5 MB)的序列化結果的總大小大於spark.driver.maxResultSize(1024.0 MB) </a> </li> <li class="side_article_list_item"> 7. <a href="http://hk.uwenku.com/question/p-sfvehlqe-rx.html" target="_blank" title="打字稿,使用類沒有構造"> 打字稿,使用類沒有構造 </a> </li> <li class="side_article_list_item"> 8. <a href="http://hk.uwenku.com/question/p-khjthsaj-pd.html" target="_blank" title="PHP通過除去空鍵"> PHP通過除去空鍵 </a> </li> <li class="side_article_list_item"> 9. <a href="http://hk.uwenku.com/question/p-nihddvmm-ne.html" target="_blank" title="Jest TypeError:無法讀取未定義的'商店'"> Jest TypeError:無法讀取未定義的'商店' </a> </li> <li class="side_article_list_item"> 10. <a href="http://hk.uwenku.com/question/p-fsfdvsjn-mm.html" target="_blank" title="在C++中有沒有一種慣用的方法來防止運行一組操作導致集合發生變化的情況?"> 在C++中有沒有一種慣用的方法來防止運行一組操作導致集合發生變化的情況? </a> </li> </ul> </div> </div> </div> <p class="article-nav-bar"></p> <div class="row sidebar article-nav"> <div class="row box_white visible-sm visible-md visible-lg margin-zero"> <div class="top"> <h3 class="title"><i class="glyphicon glyphicon-th-list"></i> 相關問題</h3> </div> <div class="article-relative-content"> <ul class="side_article_list"> <li class="side_article_list_item"> 1. <a href="http://hk.uwenku.com/question/p-ojvrhbqp-bme.html" target="_blank" title="if和else語句都會執行"> if和else語句都會執行 </a> </li> <li class="side_article_list_item"> 2. <a href="http://hk.uwenku.com/question/p-prrwohqb-ma.html" target="_blank" title="Javascript/jQuery,if和else語句都正在執行"> Javascript/jQuery,if和else語句都正在執行 </a> </li> <li class="side_article_list_item"> 3. <a href="http://hk.uwenku.com/question/p-orlfmnxh-bdw.html" target="_blank" title="if else語句執行"> if else語句執行 </a> </li> <li class="side_article_list_item"> 4. <a href="http://hk.uwenku.com/question/p-exmcqkbj-kz.html" target="_blank" title="nodejs - IF和ELSE都執行"> nodejs - IF和ELSE都執行 </a> </li> <li class="side_article_list_item"> 5. <a href="http://hk.uwenku.com/question/p-ycjandnj-bdw.html" target="_blank" title="Rails執行if和else語句"> Rails執行if和else語句 </a> </li> <li class="side_article_list_item"> 6. <a href="http://hk.uwenku.com/question/p-pbbsjxnm-dv.html" target="_blank" title="if和else語句在Stata中執行"> if和else語句在Stata中執行 </a> </li> <li class="side_article_list_item"> 7. <a href="http://hk.uwenku.com/question/p-egmtrgcl-beg.html" target="_blank" title="Javascript - 執行if和else語句"> Javascript - 執行if和else語句 </a> </li> <li class="side_article_list_item"> 8. <a href="http://hk.uwenku.com/question/p-smttarod-bez.html" target="_blank" title="兩個if和else語句執行"> 兩個if和else語句執行 </a> </li> <li class="side_article_list_item"> 9. <a href="http://hk.uwenku.com/question/p-ufahufyo-dw.html" target="_blank" title="if和else語句都在while循環中執行"> if和else語句都在while循環中執行 </a> </li> <li class="side_article_list_item"> 10. <a href="http://hk.uwenku.com/question/p-qubrzmnr-eu.html" target="_blank" title="jquery if else語句"> jquery if else語句 </a> </li> </ul> </div> </div> </div> </div> </div> </div> </div><!-- wrap end--> <!-- footer --> <footer id="footer"> <div class="bg-simple lt"> <div class="container"> <div class="row padder-v m-t"> <div class="col-xs-8"> <ul class="list-inline"> <li><a href="http://hk.uwenku.com/contact">聯系我們</a></li> <li>© 2020 HK.UWENKU.COM</li> <li><a target="_blank" href="https://beian.miit.gov.cn/">沪ICP备13005482号-4</a></li> <li><script type="text/javascript" src="https://v1.cnzz.com/z_stat.php?id=1280101193&web_id=1280101193"></script></li> <li><a href="http://www.uwenku.com/" target="_blank" title="优文库">简体中文</a></li> <li><a href="http://hk.uwenku.com/" target="_blank" title="優文庫">繁體中文</a></li> <li><a href="http://ru.uwenku.com/" target="_blank" title="поле вопросов и ответов">Русский</a></li> <li><a href="http://de.uwenku.com/" target="_blank" title="Frage - und - antwort - Park">Deutsch</a></li> <li><a href="http://es.uwenku.com/" target="_blank" title="Preguntas y respuestas">Español</a></li> <li><a href="http://hi.uwenku.com/" target="_blank" title="कार्यक्रम प्रश्न और उत्तर पार्क">हिन्दी</a></li> <li><a href="http://it.uwenku.com/" target="_blank" title="IL Programma di chiedere Park">Italiano</a></li> <li><a href="http://ja.uwenku.com/" target="_blank" title="プログラム問答園区">日本語</a></li> <li><a href="http://ko.uwenku.com/" target="_blank" title="프로그램 문답 단지">한국어</a></li> <li><a href="http://pl.uwenku.com/" target="_blank" title="program o park">Polski</a></li> <li><a href="http://tr.uwenku.com/" target="_blank" title="Program soru ve cevap parkı">Türkçe</a></li> <li><a href="http://vi.uwenku.com/" target="_blank" title="Đáp ứng viên">Tiếng Việt</a></li> <li><a href="http://fr.uwenku.com/" target="_blank" title="Programme interrogation Park">Française</a></li> </ul> </div> </div> </div> </div> </div> </footer> <!-- / footer --> <script> var _hmt = _hmt || []; (function() { var hm = document.createElement("script"); hm.src = "https://hm.baidu.com/hm.js?f78a970f17b19a79fc477a3378096f29"; var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(hm, s); })(); </script> </body> </html>