需要更改spry菜单栏中父标记元素的颜色,然后更改当前浏览器URL

Need to change the color of the parent tag element in spry menu bar followed by current browser URL

本文关键字:然后 颜色 浏览器 URL 菜单栏 spry 元素      更新时间:2023-09-26

我尝试过很多方法,比如活动标签、JavaScript等。

 <ul id="MenuBar1" class="MenuBarHorizontal">
   <li><a href="index.php" class="MenuBarItemSubmenu">Home</a>    </li>
   <li><a class="MenuBarItemSubmenu" href="#">Exam</a>
    <ul>
      <li><a href="english.php">English</a></li>
      <li><a href="math.php">Math</a></li>
      <li><a href="details.php">Details</a></li>
    </ul>
   </li>
   <li><a href="uklife.php" class="MenuBarItemSubmenu">Uk Life</a>
</ul>

当我在index.php"主页"中时,文本颜色会有所不同。当使用english.php、math.php或detauls.php时,"Exam"文本颜色会有所不同,依此类推。

最后,为了突出显示当前的url菜单,我得到了这段代码。我在工作。

<script type="text/javascript">
    var url=window.location.href;
    var elems = document.querySelectorAll('.MenuBarItemSubmenu'),elem,desiredElemContents;
    if (url.indexOf('index.php')>-1) 
      desiredElemContents = 'Home';
    else  if(url.indexOf('english.php')>-1||url.indexOf('math.php')>-1 ||url.indexOf('details.php')>-1)
      desiredElemContents = 'Exam';
    else if (url.indexOf('uklife.php')>-1) 
      desiredElemContents = 'Uk Life';
    for(var i=elems.length-1; i>=0; i--) 
      {
        var elemContents = elems[i].textContent || elems[i].innerText;
        if(elemContents!==desiredElemContents) continue;
        elem = elems[i];
        break;
       };
   if (elem) 
    { 
    elem.style.backgroundColor= "#ce0808";
    elem.style.color="white";
    }
</script>