-3

我需要放置一个名为add的按钮。用户点击后,应该会变成added

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

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

4

2 回答 2

4

做这个:

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

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

于 2013-07-20T16:53:49.427 回答
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>
于 2013-07-20T16:26:20.273 回答