2017-08-07 77 views
-4

如何创建2个输入框来替换JavaScript文本?用javascript输入框替换文本?

改为每更换一次编辑代码不同的文本 我要的是一个小的UI与输入框和输入框B和一个按钮

第一个输入框将包含文本,以取代(箱A)第二个与替换内容(框B)和onClick文本将被替换。

例如将苹果从此文本中替换为“这是一个红苹果”。

.replace (/apple/, "grape") 

.replace (/contents of box a/, "contents of box b") ///what I want to do 
+0

你有什么迄今所做? –

+1

你可以分享你的HTML和脚本代码到目前为止你尝试过吗? –

+0

因此,您添加两个文本框并引用值.... – epascarello

回答

0

我不知道如果我的理解(也许尝试添加更多详细信息的问题),但是这可能给你一个线索或两个

var input_a = document.querySelector('#input_a'); 
var input_b = document.querySelector('#input_b'); 

input_a.addEventListener('change', do_replace); 
input_a.addEventListener('keyup', do_replace); 

function do_replace() { 
    var value_a = input_a.value; 
    var replaced_a = value_a.replace('wat you dont want', 'what you want'); 
    var input_b = replaced_a; 
} 
0

尝试这样的:

<!DOCTYPE html> 
 
<html> 
 
<body> 
 

 
<p id="demo">This is apple. This is contents of box a.</p> 
 

 
<button onclick="myFunction()">Try it</button> 
 

 
<script> 
 
function myFunction() { 
 
var str = document.getElementById("demo").innerHTML; 
 
str = str.replace("apple", "grape"); 
 
str = str.replace("contents of box a", "contents of box b"); 
 
document.getElementById("demo").innerHTML = str; 
 
} 
 
</script> 
 

 
</body> 
 
</html>

+0

它不是我想要的,但用户界面将文本替换 – Marksyw