Javascript 单击时如何更改按钮的字体?
我需要放置一个名为“添加”的按钮。用户单击后,应将其更改为添加Javascript 单击时如何更改按钮的字体?,javascript,html,button,Javascript,Html,Button,我需要放置一个名为“添加”的按钮。用户单击后,应将其更改为添加 <html> <body> <script> function one() { document.write("Added"); } </script> <input type="button" value="Add" onclick="one()">
<html>
<body>
<script>
function one()
{
document.write("Added");
}
</script>
<input type="button" value="Add" onclick="one()">
</body>
</html>
功能一()
{
文件。填写(“添加”);
}
但此代码替换了我页面中的所有内容,并显示“已添加”,而我只需要更改按钮文本,而不需要更改背景。您的文档。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>
功能一(要素)
{
//您要运行的其他javascript
//您有表示发起单击事件的按钮的“元素”,您只需执行下一行即可更改文本
element.value=“已添加”;
}
执行以下操作:
<html>
<body>
<input type="button" value="Add" onclick="this.value='Added'">
</body>
</html>
jshiddle:所谓“更改…字体”,你的意思是想更改按钮上的文字(从“添加”改为“添加”),还是想更改字体系列(例如,从Arial改为Times New Roman)?他指的是文本,而不是字体,我想别忘了接受答案!