search

Home  >  Q&A  >  body text

javascript - Problem with parameters passed in settimeout

<body>
    <p class="nav">
        <ul class="nav_level_1">
            <li><a href="#">首页</a></li>
            <li><a href="#">课程大厅</a>
                <ul class="nav_level_2">
                    <li><p class="corn"></p></li>
                    <li><a href="#">JavaScript</a></li>
                    <li><a href="#">JQuery</a></li>
                </ul>
            </li>
            <li><a href="#">学习中心</a></li>
            <li><a href="#">经典案例</a></li>
            <li><a href="#">关于我们</a></li>
        </ul>
    </p>
    <script type="text/javascript">
    window.onload = function(){
        var nav_level_1 = document.getElementsByClassName("nav_level_1")[0],
            lis_1 = nav_level_1.children;
            for(var i = 0;i < lis_1.length;i++){
                lis_1[i].onmouseover = function(){
                    var ul = this.getElementsByClassName("nav_level_2")[0];
                    addHeight(ul);
                }

            }
    }
    function addHeight(ul){
        var ul_height = ul.offsetHeight;
        ul_height++;
        if(ul_height <= 95){
            ul.style.height = ul_height + "px";
            setTimeout("addHeight('"+ul+"')",10);
        }else{
            return;
        }
    }
    </script>
</body>

Use settimeout to write the expansion and contraction of the secondary menu. Pass a function with parameters in settimeout. I don’t know how to pass it. In the video, it is written like this: setTimeout("addHeight('" ul "')",10 ) but it’s not normal. What’s the reason?

三叔三叔2759 days ago868

reply all(2)I'll reply

  • 某草草

    某草草2017-07-05 11:08:49

    setTimeout(function () {addHeight(ul)},10);

    reply
    0
  • 漂亮男人

    漂亮男人2017-07-05 11:08:49

    setTimeout The first parameter of this function can accept a function or a piece of code. If it is code, put it into eval for execution.

    The way you write here is the way you write a piece of code. Please check whether the quotation marks are written correctly in one-to-one correspondence. Or change to the following two writing methods and it should work normally

    setTimeout(function () {
        addHeight(ul)
    }, 10);
    setTimeout(addHeight, 10, ul)

    reply
    0
  • Cancelreply