亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

js函數中this的傳值的問題

js函數中this的傳值的問題

DIEA 2018-09-04 09:10:06
<html><head>    <meta charset="UTF-8">    <title>Document</title>    <script type="text/javascript">        var i=1;    window.onload = function()    {   node_name = document.body.firstChild;        node_name.setAttribute("src","images/sh_hero_1.jpg");        node_name.setAttribute("onclick","c(this)");    }    function c(q)    {        i++;        q.setAttribute("src","images/sh_hero_"+i+".jpg");    }    </script></head><body><img/></body></html>node_name.setAttribute("onclick","c(this)");中的this工作原理是什么?為什么能把node_name這個局部變量傳出去?新人小白求輕拍。
查看完整描述

1 回答

?
蕭十郎

TA貢獻1815條經驗 獲得超13個贊

<button onclick="c(this)"></button><script>function c(el){    //此時el引用的是button}</script>

你的代碼node_name.setAttribute("onclick","c(this)");其實是動態地添加類似上面button中的onclick屬性。
當按下按鈕時,會執行onclick中的代碼,在c(this)這段短短的代碼中,this就是指向當前元素。


查看完整回答
反對 回復 2018-10-09
  • 1 回答
  • 0 關注
  • 1435 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號