本文實例講述了JavaScript動態修改網頁元素內容的方法。分享給大家供大家參考。具體分析如下:
下面的JS代碼當用戶點擊提交按鈕後,通過元素的textContent或者innerHTML動態指定元素的內容
<script type="text/javascript"> function showCard() { var message = document.getElementById("CCN").value; var element = document.getElementById("mycreditcardnumber"); element.textContent = message; //for Firefox element.innerHTML = message; //for IE (why can't we all just get along?) return true; } </script> <form name="dynamic" method="get"> <span>Enter Your Credit Card Number:</span> <input type="text" id="CCN" name="CCN" value="CCN" /> <input type="button" value="submit" onclick="showCard()" /> </form> <p>Verify your Credit Card Number: <span id="mycreditcardnumber">0000-00-0000-00 </span> </p>
希望本文所述對大家的javascript程序設計有所幫助。