单击时如何更改按钮的字体?

时间:2013-07-20 15:58:48

标签: javascript html button

我需要添加一个名为添加的按钮。用户点击后,应该更改为已添加

<html>
<body>
     <script>
     function one()
     {
         document.write("Added");
     }    
     </script>                   
     <input type="button" value="Add" onclick="one()">
</body>
</html>

但是这段代码替换了我页面中的所有内容并说“添加”,而我只需要更改按钮文本,而不需要在后台进行任何更改。

2 个答案:

答案 0 :(得分:4)

这样做:

<html>
<body>                   
<input type="button" value="Add" onclick="this.value='Added'">
</body>
</html>

JSfiddle:http://jsfiddle.net/7w5AQ/

答案 1 :(得分:1)

你的document.write做得比你想要的要多得多。

你可以这样做:

<html>
    <head>
     <script>
         function one(element)
         {
             //Your other javascript that you want to run
             // You have 'element' that represents the button that originated the click event and you can just do the next line to change the text
             element.value = "Added";
         }    
         </script>             
    </head>
    <body>

         <input type="button" value="Add" onclick="one(this);">
    </body>
</html>
相关问题