文本区域中的 JavaScript 字符串替换

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18387540/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 11:43:17  来源:igfitidea点击:

JavaScript string replace in textarea

javascriptstringreplace

提问by user1882876

I'm trying to create a simple page that "corrects" YouTube's old embed code so that it works in PowerPoint 2010. If I hard code the embed code into a textarea on the page, it works fine, but if the user pastes the embed code into the text area, the javaScript doesn't seem to run.

我正在尝试创建一个简单的页面来“纠正”YouTube 的旧嵌入代码,以便它在 PowerPoint 2010 中工作。如果我将嵌入代码硬编码到页面上的 textarea 中,它工作正常,但如果用户粘贴嵌入代码进入文本区域,javaScript 似乎没有运行。

Take a look here:http://jsfiddle.net/pch8N/

看看这里:http: //jsfiddle.net/pch8N/

Here's what I have so far:

这是我到目前为止所拥有的:

<p>Click the button to "fix" the old embed code</p>

<textarea rows="10" cols="60" id="demo"></textarea>
<br/>

<script>
function myFunction()
{
var str=document.getElementById("demo").innerHTML; 
var n=str.replace(/\/\/www.youtube/g,"http://www.youtube").replace(/version=3/g,"version=2");
document.getElementById("demo").innerHTML=n;
}
</script>

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

Thanks for the help!

谢谢您的帮助!

采纳答案by null.point3r

You should use valueinstead of innerHtml

你应该使用value而不是innerHtml

function myFunction2()
{
var str=document.getElementById("demo2").value; 
var n=str.replace(/\/\/www.youtube/g,"http://www.youtube").replace(/version=3/g,"version=2");
document.getElementById("demo2").value=n;
}

回答by Shadow

Put your textarea inside form tag

将你的 textarea 放在表单标签中

Then use

然后使用

var str=document.getElementById("demo").value; 

回答by selim

        // hope this would be usefull
        // i used these codes for auto completing the texts in textarea.
        // other methods change all matching items before the selected text
        // but this affects only the text where caret on.
        // at first, i divided textarea.value into 3 pieces. these are ;
        // p1; until the 'searched' item, p2 after 'searched' item
        // and pa = new item that will replaced with 'searched' item
        // then, i combined them together again.

        var tea = document.getElementById(targetTextArea);

        caretPosition = tea.selectionStart - ara.length; //ara=searched item
        p1 = tea.value.substring(0,caretPosition);
            console.log('p1 text : ' + p1);
        p2 = tea.value.substring(caretPosition+ara.length,tea.value.length);
            console.log('p2 text : ' + p2);
        pa = yeni;  //new item
            console.log('pa text : ' + pa);

        tea.value = p1 + pa + p2;

        tea.selectionStart = caretPosition + yeni.length;
        tea.selectionEnd = caretPosition + yeni.length;

        tea.focus();

回答by Shamim Hafiz

var str=document.getElementById("demo").innerHTML; 

You should use .valueand not .innerHTML

你应该使用.value而不是.innerHTML