简单的Javascript对html文本框转换.不能工作两次
Simple Javascript on html textbox conversion. Doesn't work twice
我正在使用replace方法,如果我输入"test test",只有第一个测试被转换为good,所以它将成为"good test"。我不明白为什么会发生这种事。附带的问题是,如果我要添加20个我想替换的单词,我需要创建20个不同的str。replace吗?
<!DOCTYPE html>
<html>
<body>
<p>Click the button to replace "Test" with "Good"</p>
<textarea id="firstbox"></textarea>
<textarea id="secondbox"></textarea>
<button onclick="myFunction()">Change</button>
<script>
function myFunction() {
var str = document.getElementById("firstbox").value.toLowerCase()
var res = str.replace("test", "good");
document.getElementById("secondbox").value = res;
}
</script>
</body>
</html>
如有任何帮助,不胜感激
使用正则表达式,将"good"
改为/good/g
function myFunction() {
var str = document.getElementById("firstbox").value.toLowerCase()
var res = str.replace(/test/g, "good");
document.getElementById("secondbox").value = res;
}
<p>Click the button to replace "Test" with "Good"</p>
<textarea id="firstbox"></textarea>
<textarea id="secondbox"></textarea>
<button onclick="myFunction()">Change</button>
相关文章:
- button.单击两次删除附加操作后不工作
- JavaScript onclick在IE 9中工作两次,然后停止
- JQuery脚本没有'Don’我不能工作两次
- 在页面中使用了两次多个图像上传,但第一个正在工作,另一个不起作用
- 地图,rails 4.2,javascript,鼠标悬停,只工作一次(或两次).然后在重新加载之后
- 当audiojs加载两次时,audiojs进度条不工作
- rails4-javascript文件需要调用两次才能工作
- 新的回顾;I don’在(基本上)同一页上做两次工作
- 为什么我的内容在从 1.5.26 迁移后两次可见,而 JavaScript 在 Joomla 3.x 上无法正常工作
- 滚动时加载更多 滚动速度时一次工作两次
- Jquery Click 事件工作两次
- Javascript 在两次 HTML 注入后停止工作
- Jquery点击功能只工作两次
- 向同一按钮添加两次单击事件只能工作一次
- 为什么这个Javascript图表不会工作两次
- 简单的Javascript对html文本框转换.不能工作两次
- jQuery滑块只工作两次
- Ajax调用with只能工作两次
- 滑块不能在同一页面上工作两次
- 使HTML5画布中的重力工作两次