需求:點選button取得input框中選取的內容,讓選取的內容變紅,
實作:程式碼如下
<!DOCTYPE HTML> <html> <head> <meta http-equiv="content-type" content="text/html;charset=utf-8"> <title>Test</title> <style type="text/css"> div{ display: none; } </style> </head> <body> <input type="text" id="txt" value="" /> <input type="button" value="获取文本框内的值" id="btn" onclick="getText();"/> <div id="showText"> </div> <script type="text/javascript"> function getText () { var showText = document.getElementById("showText"); showText.style.display = "block"; showText.style.color = "red"; showText.innerHTML=document.getElementById("txt").value; } </script> </body> </html>
效果: