javascript - settimeout中傳入?yún)?shù)的問題
問題描述
<body> <p class='nav'><ul class='nav_level_1'> <li><a href='http://www.aoyou183.cn/wenda/4071.html#'>首頁(yè)</a></li> <li><a href='http://www.aoyou183.cn/wenda/4071.html#'>課程大廳</a><ul class='nav_level_2'> <li><p class='corn'></p></li> <li><a href='http://www.aoyou183.cn/wenda/4071.html#'>JavaScript</a></li> <li><a href='http://www.aoyou183.cn/wenda/4071.html#'>JQuery</a></li></ul> </li> <li><a href='http://www.aoyou183.cn/wenda/4071.html#'>學(xué)習(xí)中心</a></li> <li><a href='http://www.aoyou183.cn/wenda/4071.html#'>經(jīng)典案例</a></li> <li><a href='http://www.aoyou183.cn/wenda/4071.html#'>關(guān)于我們</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>
用settimeout寫二級(jí)菜單的展開收縮,在settimeout中傳入一個(gè)帶有參數(shù)的函數(shù),不知道怎么傳,視頻中是這樣寫的setTimeout('addHeight(’'+ul+'’)',10)但是不太正常,請(qǐng)問是什么原因?
問題解答
回答1:setTimeout(function () {addHeight(ul)},10);回答2:
setTimeout 這個(gè)函數(shù)第一個(gè)參數(shù)可以接受一個(gè) 函數(shù) 或者 一段代碼,如果是代碼,則將它放進(jìn)eval執(zhí)行。
你這里的寫法就是一段代碼的寫法,你檢查一下引號(hào)的書寫,是不是一一對(duì)應(yīng)的寫的正確。或者換成下面的兩個(gè)寫法都應(yīng)該可以正常工作
setTimeout(function () { addHeight(ul)}, 10);
setTimeout(addHeight, 10, ul)
相關(guān)文章:
1. 我的html頁(yè)面一提交,網(wǎng)頁(yè)便顯示出了我的php代碼,求問是什么原因?2. tp6表單令牌3. docker 17.03 怎么配置 registry mirror ?4. java 排序的問題5. 我在centos容器里安裝docker,也就是在容器里安裝容器,報(bào)錯(cuò)了?6. django - 后臺(tái)返回的json數(shù)據(jù)經(jīng)過Base64加密,獲取時(shí)用python如何解密~!7. node.js - node 客戶端socket一直報(bào)錯(cuò)Error: read ECONNRESET,用php的socket沒問題哈。。8. 數(shù)據(jù)庫(kù) - 使用讀寫分離后, MySQL主從復(fù)制延遲會(huì)導(dǎo)致讀不到數(shù)據(jù)嗎?9. 老哥們求助啊10. javascript - canvas 可以實(shí)現(xiàn) PS 魔法橡皮擦的功能嗎?
