【问题标题】:Can you for loop through <li> elements and apply a mouseover function?你可以循环遍历 <li> 元素并应用鼠标悬停功能吗?
【发布时间】:2021-03-17 11:32:55
【问题描述】:

试图做到这一点,当您将鼠标悬停在导航中的某个项目上时,它会在悬停时变为粗体。我知道用其他语言可以做到这一点,但我正在尝试学习 JS。

<body>
        <p>
        If you ask I will give you:
        </p>
        <ul id = 'nav_1'>
            <li>Some nice chocolate cake</li>
            <li>A fresh-brewed cup of coffee</li>
            <li>An honest compliment</li>
            <li>A brand new Porsche</li>
        </ul>
        <script type="text/javascript">
            "use strict";   
            const nav1 = document.getElementByID('nav_1');
            const listItems = nav1.getElementsByTagName("li");
            
            for(let i = 0;i<listItems.length;i++){
                listItems[i].onmouseover = function(){
                    document.listItems[i].style.fontWeight = "bold";
                }
                listItems[i].onmouseleave = function(){
                    document.listItems[i].style.fontWeight = "normal";
                }
            }
        </script>
    </body>

【问题讨论】:

  • 嗨迈克尔,如果以下答案之一解决了您的问题,请接受其中一个答案以完成此问题。太棒了!

标签: javascript html loops onmouseover


【解决方案1】:

嗨,迈克尔,欢迎来到 StackOverflow。

为此,我建议您使用 CSS 而不是 JS。这样更容易更高效

#nav_1 li:hover { /* Bold li element on hoverin nav_1 elements */
    font-weight: bold;
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <p>
    If you ask I will give you:
    </p>
    <ul id = 'nav_1'>
        <li>Some nice chocolate cake</li>
        <li>A fresh-brewed cup of coffee</li>
        <li>An honest compliment</li>
        <li>A brand new Porsche</li>
    </ul>
</body>
</html>

这将是 JS 解决方案。您的问题出在回调中。 document 没有名为 listItems 的变量,您可能将其误认为是 window。 getElementByID也有错别字,一定是getElementById,JS函数区分大小写。

<body>
    <p>
    If you ask I will give you:
    </p>
    <ul id = 'nav_1'>
        <li>Some nice chocolate cake</li>
        <li>A fresh-brewed cup of coffee</li>
        <li>An honest compliment</li>
        <li>A brand new Porsche</li>
    </ul>
    <script type="text/javascript">
        "use strict";
        const nav1 = document.getElementById('nav_1');
        const listItems = nav1.getElementsByTagName("li");

        for(let i = 0;i<listItems.length;i++){
            listItems[i].onmouseover = function(e){
                listItems[i].style.fontWeight = "bold";
            }
            listItems[i].onmouseleave = function(e){
                listItems[i].style.fontWeight = "normal";
            }
        }
    </script>
</body>

【讨论】:

    【解决方案2】:

    getElementByID 有一个小错误,最后一个字母应该是小写的d。 然后删除listItems 变量上的document。

    正如 Vitalij 所指出的,CSS 将是更好的处理途径(不需要编写脚本),但要保持 javascript:

    "use strict";
    
    const nav1 = document.getElementById('nav_1');
    const listItems = nav1.getElementsByTagName("li");
    
    for(let i = 0; i < listItems.length; i++){
        listItems[i].onmouseover = function(){
           listItems[i].style.fontWeight = "bold";
        }
        listItems[i].onmouseleave = function(){
           listItems[i].style.fontWeight = "normal";
        }
    }
    <p>
    If you ask I will give you:
    </p>
    <ul id='nav_1'>
        <li>Some nice chocolate cake</li>
        <li>A fresh-brewed cup of coffee</li>
        <li>An honest compliment</li>
        <li>A brand new Porsche</li>
    </ul>

    【讨论】:

      猜你喜欢
      • 2013-10-30
      • 1970-01-01
      • 2021-03-20
      • 2011-07-07
      • 2016-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-28
      相关资源
      最近更新 更多