這代碼為什么沒有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>無標(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)頁新打開后開始加載網(wǎng)頁文件。對于HTML標(biāo)簽加載后就顯示到頁面中。而對于script腳本,也會(huì)加載執(zhí)行。就像本例中,當(dāng)網(wǎng)頁加載到createa("http://idcbgp.cn","慕課網(wǎng)");時(shí),就會(huì)調(diào)用相關(guān)函數(shù)實(shí)現(xiàn)跳轉(zhuǎn)。也就是說本來script標(biāo)簽內(nèi)的語句是默認(rèn)執(zhí)行的。但是function定義的需要調(diào)用才能執(zhí)行,而這個(gè)最后一句話就正好調(diào)用了這個(gè)函數(shù)。