简体   繁体   English

替换textarea脚本+ RTE中的文本

[英]Replace text in textarea script + RTE

I have a javascript code that replace some words writen in text area. 我有一个javascript代码,可以替换在文本区域中写的一些单词。 Eg. 例如。 If I write word "dog" it will change it to word "cat". 如果我写单词“ dog”,它将更改为单词“ cat”。

<html>
<head>
<script>
function changeText(){
dt=document.getElementsByTagName("textarea")[0];
dt.value=dt.value.replace(/dog/g,"cat");
dt.value=dt.value.replace(/blue/g,"red");
dt.value=dt.value.replace(/good/g,"bad");
}
</script>
</head>
<body>
<textarea rows="10" cols="65"></textarea> <br>
<input type="button" value="change" onclick="changeText()">
</body>
</html>

I have two problems with this script and I don't know how to fix it. 这个脚本有两个问题,我不知道如何解决。

  1. How can I call external file with words for replacement? 如何用替换词调用外部文件? I will have 200+ words and it will better that I have them in separate file. 我将有200多个字,最好将它们放在单独的文件中。
  2. How to implement some simple RTE (eg. Nicedit) and still have working script? 如何实现一些简单的RTE(例如Nicedit)并仍然具有正常工作的脚本? I try it and script doesn't work... 我尝试了,脚本不起作用...

Thanks in advance for help! 在此先感谢您的帮助! :) :)

  1. Due to security reasons, your possibilites are limited. 出于安全原因,您的可能性有限。 The best way to do it is to load txt/json/xml file from your server using AJAX technology. 最好的方法是使用AJAX技术从服务器加载txt / json / xml文件。 Keep in mind that it's an asynchronous method. 请记住,这是一个异步方法。 Without JQuery (most popular JS library), with pure Javascript, you can achieve it with the code below: 如果没有JQuery(最受欢迎的JS库)和纯Javascript,则可以通过以下代码实现:

     var fileContent = ''; var client = new XMLHttpRequest(); // we create a request client.open('GET', '/foo.txt'); // foo.txt is the name of the file client.onreadystatechange = function() { fileContent = client.responseText; alert("Hooray! My file was loaded, this is its content: " + fileContent ); } client.send(); // and we send a request 
  2. Every rich text editor has its own JS library and a way to get editor's content, so it really depends. 每个富文本编辑器都有其自己的JS库以及获取编辑器内容的方法,因此这取决于实际情况。 For nicedit it's like this (assuming your textarea has an ID): 对于nicedit来说就是这样(假设您的textarea有一个ID):

     var nicInstance = nicEditors.findEditor('idOfYourTextarea'); var notes = nicInstance.getContent(); // voila - your content 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM