這代碼為什么沒(méi)有onclick點(diǎn)擊事件,點(diǎn)擊慕課網(wǎng)也跳轉(zhuǎn)到別的網(wǎng)址了呢?
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>無(wú)標(biāo)題文檔</title>
</head>
<body>
<script type="text/javascript">
var main = document.body;
//創(chuàng)建鏈接
function createa(url,text)
{
? var z=document.createElement("a");
? z.href=url;
? z.innerHTML=text;
? z.style.color="red";
? main.appendChild(z);?
}
// 調(diào)用函數(shù)創(chuàng)建鏈接
? ? createa("http://idcbgp.cn","慕課網(wǎng)");
</script>?
</body>
</html>
2018-09-17
網(wǎng)頁(yè)新打開(kāi)后開(kāi)始加載網(wǎng)頁(yè)文件。對(duì)于HTML標(biāo)簽加載后就顯示到頁(yè)面中。而對(duì)于script腳本,也會(huì)加載執(zhí)行。就像本例中,當(dāng)網(wǎng)頁(yè)加載到createa("http://idcbgp.cn","慕課網(wǎng)");時(shí),就會(huì)調(diào)用相關(guān)函數(shù)實(shí)現(xiàn)跳轉(zhuǎn)。也就是說(shuō)本來(lái)script標(biāo)簽內(nèi)的語(yǔ)句是默認(rèn)執(zhí)行的。但是function定義的需要調(diào)用才能執(zhí)行,而這個(gè)最后一句話就正好調(diào)用了這個(gè)函數(shù)。