Javascript:替换textarea中特定关键字的空格

时间:2017-11-10 10:51:51

标签: javascript input replace textarea keyword

我需要你帮助编写一个javascript函数来用textarea中的其他单词替换特定的单词。

我想在textarea中找到并替换放在输入文本上的单词之间的空格:

例如:

<html>
<head>
<title>Replace Specific words in textarea</title>
</head>
<body>
<Form name="frm" method="post" id="frm">
<TextArea name="txtArea1" cols="85" rows="10" ID="Textarea1">
While it was clear that forwarding the material was naughty (and the "culprits" had their wrists smacked), there.
This is one of the#main#ways of using link events. If you have not seen rollover images before, they are images 
</TextArea><br>
Input keywords: <Input type="text" name="findwords" value="the main ways" id="text1">
<Input type="submit" name="btnSubmit" value="Submit" id="submit">
</form>
</body>
</html>

非常感谢任何帮助

鲍勃

1 个答案:

答案 0 :(得分:0)

不确定理解,但试试这个:

// Execute instructions when the button with ID "submit" is clicked.
document.getElementById('submit').addEventListener('click', function()
{
    // Get user inputs.
    var search = document.getElementById('text1').value;
    // Replace spaces and tabs with a regular expression.
    var alteredSearch = search.replace(/\s/g, '#');
    // Get the textarea content.
    var look = document.getElementById('Textarea1').value;

    // Finally, replace the textarea content.
    document.getElementById('Textarea1').value = look.replace(new RegExp(search, 'g'), alteredSearch);
});

我看到你有一张表格。目前,如果您点击提交按钮,您的页面将重新加载,并且不会执行任何操作。如果您希望javascript正常运行,请将输入的类型属性设置为button