如何替换 html5 中的输出文本。
例如,用户输入这个文本:我的名字是托尼。
我想把输出文本:“Toni”替换成Ani,比如输出,变成:“我叫Ani”。
这是我的代码:
<title>textBoxes.html</title>
<script type = "text/javascript">
// from textBoxes.html
function repleace(){
var txtName = document.getElementById("txtName");
var txtOutput = document.getElementById("txtOutput");
var name = txtName.value;
if(name == "toni"){
name = "ani";
}
txtOutput.value = name;
}
</script>
<link rel = "stylesheet"
type = "text/css"
href = "textBoxes.css" />
</head>
<body>
<h1>Text Box Input and Output</h1>
<form action = "">
<fieldset>
<label>Type your name: </label>
<input type = "text"
id = "txtName" />
<input type = "button"
value = "GO"
onclick = "repleace()"/>
<input type = "text"
id = "txtOutput" />
</fieldset>
</form>
</body>
</html>
这段代码只是在我输入文本时替换文本:Toni,但是当我输入:我的名字是 Toni 时。无法替换文本,Toni。
最佳答案
使用替换功能。
name.replace("toni", "Ani");
关于javascript - 替换html5中文本框的输出文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35896936/