使用setAttribute和直接創(chuàng)建屬性效果不同???
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>無標題文檔</title>
</head>
<body>
<script type="text/javascript">
var main = document.body;
//創(chuàng)建鏈接
function createa(url,text)
{
? ? var li=document.createElement("a");
? ? /*li.setAttribute("href","url");
? ? li.setAttribute("innerHTML","text");*/
? ? //為什么使用注釋掉的內(nèi)容就不能運行呢?注釋掉的內(nèi)容哪里有錯誤呢?
? ? li.href=url;
? ? li.innerHTML=text;
? ? li.style.color="red";
? ? main.appendChild(li);
}
// 調(diào)用函數(shù)創(chuàng)建鏈接
createa("http://idcbgp.cn/","慕課網(wǎng)");
</script>?
</body>
</html>
2017-08-14
obj.setAttribute("attr","value")是DOM1級,而直接obj.attr=value是DOM0級。對于DOM(元素節(jié)點)對象使用setAttribute是W3C標準,兼容性更好.
2016-03-20
li.innerHTML是li元素的html內(nèi)容,并不是他的屬性,所以要報錯。
2016-02-02
http://idcbgp.cn/qadetail/57746看看這個問題吧,還有就是你用了注釋的內(nèi)容就不需要下面這兩行代碼了
?li.href=url;
? ? li.innerHTML=text;
2016-01-27
使用setAttribute出不來效果,改用直接增加屬性的方法就可以用,是我在寫setAttribute方法時,哪里寫錯了嗎?