javascript - Problem with parameters passed in settimeout
三叔
三叔 2017-07-05 11:06:49
0
2
804
<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?

三叔
三叔

reply all(2)
某草草
setTimeout(function () {addHeight(ul)},10);
漂亮男人

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)
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!