為什么會(huì)出現(xiàn) [object HTMLParagraphElement]
?代碼:function hiddentext()
? ? {
? ? var mychar = document.getElementById("con");
? ? document.write(mychar).style.display="none";
? ? }
? ? function showtext(){
? ? var mychar = document.getElementById("con");
? ? document.write(mychar).style.display="block";
? ? }
</script>
</head>
<body>
<h1>JavaScript</h1> ?
? ? ? ? <p id="con">做為一個(gè)Web開發(fā)師來說,如果你想提供漂亮的網(wǎng)頁、令用戶滿意的上網(wǎng)體驗(yàn),JavaScript是必不可少的工具。</p>?
? ? ? ? <form>
? ? ? ? ? ? <input type="button" onclick="hiddentext()" value="隱藏內(nèi)容" />?
? ? ? ? ? ? <input type="button" onclick="showtext()" value="顯示內(nèi)容" />?
? ? ? ? </form>
</body>
2016-09-29
mychar = document.getElementById("con");只是取到了標(biāo)簽<p>,而不能取到標(biāo)簽中的內(nèi)容,所以會(huì)出現(xiàn)
[object HTMLParagraphElement]錯(cuò)誤
2016-10-03
var mychar =?document.getElementById("con"); 已經(jīng)獲取到了id為con的對(duì)象<p>,并把它賦值在變量 mychar 里了
后邊的 隱藏和顯示效果應(yīng)該是對(duì)mychar作用的
所以改換成
mychar.style.display="none"; ? 就行
document.write(mychar) 是輸出變量 mychar 的意思吧
2016-09-29
改成上述形式即可