JavaScript 更改 ID 不起作用

标签 javascript html css

我似乎无法使用函数 changeTab(num) 将 li 元素的 ID 从 number 更改为 selected,并将所选选项卡的 ID 恢复为其默认编号。它只工作一两次然后停止。我的目标是模仿选中和未选中选项卡的变化,例如在 Chrome 选项卡中。

<ul id="nav">
                    <li onClick="changeTab(1);" id="1"><a  href="#">Nav 1</a></li>
                    <li onClick="changeTab(2);" id="2"><a  href="#">Nav 2</a></li>
                    <li onClick="changeTab(3);" id="selected"><a href="#">Nav 3</a></li>
                    <li onClick="changeTab(4);" id="4"><a  href="#">Nav 4</a></li>
</ul>

我的 JavaScript 代码是:

function changeTab(num){

    switch(num){
        case 1:
            document.getElementById("selected").id = "1";
            break;
        case 2:
            document.getElementById("selected").id = "2";
            break;
        case 3:
            document.getElementById("selected").id = "3";
            break;
        case 4:
            document.getElementById("selected").id = "4";
            break;
        default:
            document.getElementById("selected").color = "";
    }


    //
    document.getElementById(num).id = "selected";

最佳答案

EDIT 按照 WTK 的建议(作为上面问题中的评论)这是有效的 HTML,id 值必须以字母而不是数字开头......我已经更新了我的答案通过在 id 前加上 nav-...

成为有效的 HTML
<ul id="nav">
    <li onclick="changeTab(this);" id="nav-1"><a  href="#">Nav 1</a></li>
    <li onclick="changeTab(this);" id="nav-2"><a  href="#">Nav 2</a></li>
    <li onclick="changeTab(this);" id="selected"><a href="#">Nav 3</a></li>
    <li onclick="changeTab(this);" id="nav-4"><a  href="#">Nav 4</a></li>
</ul>

在 onclick 处理程序中使用 this 变量将获取被单击的元素...然后您可以使用以下函数作为处理程序...

function changeTab(el) {
  // This function is passed 'el' from the onclick handler of the li. The
  // onclick handler passes 'this' through as the 'el' argument. 'el' will 
  // be a HTMLElement object. 

  // We only want to do something if the 'el' HTMLElement object does not
  // currently have the 'id' "selected", otherwise we do nothing.
  if(el.id != "selected") {
    // Revert all tabs to their original ids

    // Try and find the HTMLElement with the id "nav". The variable 'nav'
    // will be another HTMLElement object, this time representing the ul element.
    var nav = document.getElementById("nav");

    // The function 'getElementsByTagName' always returns a 
    // HTMLElementCollection, it might have zero elements if there were no
    // matches. We can use it as an array (although there are things to
    // take into consideration that affect performance). The 
    // HTMLElementCollection will contain all li elements that are 
    // descendants of the 'nav' ul element
    var lis = nav.getElementsByTagName("li");

    // Here we do a for-loop to iterate through the element collection
    // each item in the HTMLElementCollection will be a HTMLElement
    // representing one of the li elements
    for(var i = 0; i < lis.length; ++i) { // Arrays are zero-indexed

      // We set the id to nav-n overwriting whatever was there previously
      lis[i].id = "nav-" + (i + 1); // Our tabs are one-indexed
    }

    // Set the id for the original HTMLElement that was passed into the
    // function to "selected", we do this step last as one of the li HTMLElements
    // we change in the for-loop above will also be this HTMLElement
    el.id = "selected";
  }
}

还有其他可能更好的方法可以做到这一点。不过,这应该可以解决问题,如果您想更深入地研究,我会推荐这本书 Pro JavaScript Design Patterns。

关于JavaScript 更改 ID 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12782032/

相关文章:

html - chrome 和 firefox 显示不同

jquery - 我正在努力解决的 CSS 问题?

javascript - "window.setTimeout"的 iOS 实现与 JavascriptCore

javascript - 使用 jQuery 动态添加表单函数

javascript - jQuery 选项取消选择事件?

python - Flask Python 按钮没有响应

Javascript:将标签的值传递给变量

javascript - 将数组的每个元素写入文件的新行的最佳方法是什么?

html - 缩小尺寸(掩模图像)。你能帮助我吗?

媒体查询元素上的 JQuery