4 回答

TA貢獻(xiàn)15條經(jīng)驗(yàn) 獲得超4個(gè)贊
對(duì)于bdd()方法,getElementsByName得到的是一個(gè)數(shù)組,顏色的屬性是backgroundColor而不是groundback,
get ElementsByTagName(“button‘)得到的是按鈕而不是div

TA貢獻(xiàn)3593條經(jīng)驗(yàn) 獲得超1個(gè)贊

TA貢獻(xiàn)56條經(jīng)驗(yàn) 獲得超22個(gè)贊
<style?type="text/css"> div{ ????????????color:red; ????????????background:?green; ????????????width:150px; ????????????height:?100px; ????????????border:1px?solid ????????}?? </style> <script?type="text/javascript"> ??function?add(){ ????????????var?A=document.getElementById("com"); ????????????A.style.background="yellow"; ????????????} ????????????function?bdd(){ ????????????????var?B=document.getElementsById("con"); ????????????????B.style.background="white"; ????????????} ????????????function?cdd(){ ????????????????var?C=docnment.getElementsById("button"); ????????????????C.style.background="pink"; ????????????} </script> </head> <body> ????????<div??id="com">1</div> ????????<div?id="con"?>1</div> ????????<div?id="button">3</div> ????????<button?onclick="add()"?>通過id改變背景顏色</button> ????????<button?onclick="bdd()"?>通過name改變背景顏色</button> ????????<button?onclick="cdd()"?>通過tagname改變背景顏色</button> </body>
好奇怪,改為獲取id的值都不可以變顏色,
只能變第一個(gè)
知道答案希望告訴我一下。。。

TA貢獻(xiàn)15條經(jīng)驗(yàn) 獲得超4個(gè)贊
<!DOCTYPE?HTML> <html> ? ????<head> ????????<meta?http-equiv="Content-Type"?content="text/html;?charset=utf-8"> ????????<title>getAttribute</title> ????????<!--為div設(shè)置基本樣式--> ????????<style?type="text/css"> ????????div{ ????????????color:red; ????????????background:?green; ????????????width:150px; ????????????height:?100px; ????????????border:1px?solid ????????}?? ????????</style> ?????? ????</head> ? ????<body> ????????<div?name="con"?id="com">1</div> ????????<div?name="con1"?>2</div> ????????<div?name="con"?>3</div> ????????<button?onclick="add()">通過id改變背景顏色</button> ????????<button?onclick="bdd()">通過name改變背景顏色</button> ????????<button?onclick="cdd()">通過tagname改變背景顏色</button> ??????????<script?type="text/javascript">//三種方法檢索元素節(jié)點(diǎn),并通過節(jié)點(diǎn)改變背景顏色 ????????????function?add(){ ????????????var?A=document.getElementById("com") ????????????A.style.backgroundColor="yellow" ????????????} ????????????function?bdd(){ ????????????????var?B=document.getElementsByName("con1") ????????????????B[0].style.backgroundColor="yellow" ????????????} ????????????function?cdd(){ ????????????????var?C=document.getElementsByTagName("button") ????????????????C[2].style.backgroundColor="red" ????????????} ????????</script> ????</body> ? </html>

TA貢獻(xiàn)1條經(jīng)驗(yàn) 獲得超0個(gè)贊
同學(xué),查了一下,好像div是name這個(gè)屬性是不能這樣獲取的,還有就是27行document錯(cuò)了,還有三個(gè)background屬性也都敲錯(cuò)了。

TA貢獻(xiàn)5條經(jīng)驗(yàn) 獲得超3個(gè)贊
第一點(diǎn)getElementsByName("con")獲得的是哪個(gè)div的name有歧義,因?yàn)樗玫降氖嵌鄠€(gè)name,tagname同樣是
添加回答
舉報(bào)