4

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

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

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

<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>

谢谢您的帮助!

4

4 回答 4

3

你应该使用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;
}
于 2013-08-22T18:11:15.310 回答
0
var str=document.getElementById("demo").innerHTML; 

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

于 2013-08-22T17:59:05.283 回答
0

将您的文本区域放在表单标签内

然后使用

var str=document.getElementById("demo").value; 
于 2013-08-22T18:15:19.123 回答
0
        // 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();
于 2018-11-16T09:07:54.263 回答