如果Breadrumb存在,请加载JavaScript

时间:2015-12-01 23:15:53

标签: javascript

所以我制作了一个javascript,并且只有在页面中存在某个痕迹时才想加载它,让我们说这个痕迹被称为“支持”,我的面包屑代码是:

<ol class="breadcrumb top ipsList_inline left" id="breadcrumb">
                                            <li itemscope="" itemtype="http://data-vocabulary.org/Breadcrumb" class="first">
                            <a href="https://www.website.com" itemprop="url">
                                <span itemprop="title">website</span>
                            </a>
                        </li>


                        <li itemscope="" itemtype="http://data-vocabulary.org/Breadcrumb">
                             <a href="website/section" title="Return to website" itemprop="url"><span itemprop="title">website</span></a>
                        </li>



                        <li itemscope="" itemtype="http://data-vocabulary.org/Breadcrumb">
                             <a href="website/section" title="Return to Section" itemprop="url"><span itemprop="title">Section</span></a>
                        </li>



                        <li itemscope="" itemtype="http://data-vocabulary.org/Breadcrumb">
                             <a href="website/Support/" title="Return to Support" itemprop="url"><span itemprop="title">Support</span></a>
                        </li>


                </ol>

1 个答案:

答案 0 :(得分:0)

您可以这样做:

window.onload = function () {
  var breadcrumb = document.getElementById("breadcrumb");
  var titles = breadcrumb.getElementsByTagName("span");

  var support = 0;
  for(var i = 0; i < titles.length; ++i){
    support += titles[i].innerHTML == "Support";
  }

  if(support) initialize();
}

function initialize() {
    /* Do your stuff. */
}