第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

誰能解釋下下面這段代碼的錯誤

誰能解釋下下面這段代碼的錯誤

清風(fēng)流光 2017-04-16 05:27:42
<!DOCTYPE?html> <html> <head> <meta?charset="UTF-8"> <title></title> <style> .red?{ width:?100px; height:?100px; background:?red; } .blue?{ width:?100px; height:?100px; background:?blue; } </style> <script> window.onload?=?function()?{ var?box?=?document.getElementById('box'); box.onclick?=?toBlue(); } function?toRed()?{ this.className?=?'red' this.onclick?=?toBlue() } function?toBlue()?{ this.className?=?'blue' this.onclick?=?toRed() } </script> </head> <body> <div?id='box'?class="red"?style="text-align:?center;line-height:?100px;">DIV</div> <body/> </html>為什么事件在函數(shù)里調(diào)用必須等于函數(shù)?在函數(shù)里調(diào)用值沒有執(zhí)行事件操作 為什么事件還是會自動執(zhí)行?
查看完整描述

3 回答

已采納
?
ruibin

TA貢獻358條經(jīng)驗 獲得超213個贊

?window.onload?=?function()?{

????????????????var?box?=?document.getElementById('box');

????????????????box.onclick?=?toBlue();

????????????????//這是mark1

????????????}

?

請看這段代碼。這段代碼的意思是頁面一加載完成就執(zhí)行下面的js代碼。那么當js代碼執(zhí)行到mark1的時候發(fā)生了什么呢?

會執(zhí)行toBlue。為什么?因為toBlue()。這是你寫的。這得意思是調(diào)用toBlue然后把toBlue的結(jié)果賦值給box.onclick。所以不管執(zhí)行不執(zhí)行時間,toBlue一定會執(zhí)行,因為js代碼運行到這一行了! 就是這么簡單,這和事件還扯不上關(guān)系~

查看完整回答
3 反對 回復(fù) 2017-04-16
?
西蘭花偉大炮

TA貢獻376條經(jīng)驗 獲得超318個贊

<!DOCTYPE?html>
<html>
?<head>
????????<meta?charset="UTF-8">
????????<title></title>
????????<style>
????????????.red?{
????????????????width:?100px;
????????????????height:?100px;
????????????????background:?red;
????????????}
?????????????
????????????.blue?{
????????????????width:?100px;
????????????????height:?100px;
????????????????background:?blue;
????????????}
????????</style>
????????<script>
????????????window.onload?=?function()?{
????????????????var?box?=?document.getElementById('box');
????????????????box.onclick?=?toBlue;
????????????}
?
????????????function?toRed()?{
????????????????this.className?=?'red'
????????????????this.onclick?=?toBlue;
????????????}
?
????????????function?toBlue()?{
????????????????this.className?=?'blue'
????????????????this.onclick?=?toRed;
????????????}
????????</script>
????</head>
?
????<body>
????????<div?id='box'?class="red"?style="text-align:?center;line-height:?100px;">DIV</div>
????????<body/>
?
</html>

不要加上括號

查看完整回答
2 反對 回復(fù) 2017-04-16
?
落葉1111

TA貢獻57條經(jīng)驗 獲得超9個贊

X.onclick=function(){};是自調(diào)用函數(shù)

查看完整回答
1 反對 回復(fù) 2017-04-16
?
slumdog

TA貢獻6條經(jīng)驗 獲得超4個贊

最后的body結(jié)束標簽難道不應(yīng)該是</body>嗎?

查看完整回答
1 反對 回復(fù) 2017-04-16
  • 3 回答
  • 0 關(guān)注
  • 1637 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號