textarea将jquery附加到iframe无法正常工作

时间:2016-09-24 11:02:13

标签: jquery iframe editor

<script>
function full(){
return $('.iframes').contents().find('body').html($('.value').val());
}
</script>

<textarea class="value"></textarea>
<button onClick="full();">click</button>
<iframe class='iframes'></iframe>

我正在尝试为jquery创建在线编辑器,所以首先创建

<textarea class="value"></textarea>
<button onClick="full();">click</button>
<iframe class='iframes'></iframe>

和jquery是

function full(){
return $('.iframes').contents().find('body').html($('.value').val());
}

当我在textarea中输入任何内容时,它会附加到iframe上,但我的问题是当我在textarea中输入jquery函数时就像那样

<p id="demo">test</p>
<button onclick="fff();">Click Me!</button>
<script>
function fff() {
$('#demo').text('my name is');
}
</script>

jquery无法正常工作。是在那里让它在iframe中工作

1 个答案:

答案 0 :(得分:0)

我的问题是dom所以我写这段代码并且工作得很好

<textarea id="mytext" onKeyUp="fff();"></textarea>
<div id="sss"></div>
<script>
function fff() {
    var text = document.getElementById("mytext").value;
    var ifr = document.createElement("iframe");
    ifr.setAttribute("frameborder", "0");
    ifr.setAttribute("id", "iframeResult");
    document.getElementById("sss").innerHTML = "";
    document.getElementById("sss").appendChild(ifr);
    var ifrw = (ifr.contentWindow) ? ifr.contentWindow :
    (ifr.contentDocument.document) ? ifr.contentDocument.document : ifr.contentDocument;
    ifrw.document.open();
    ifrw.document.write(text);
    ifrw.document.close();
    if (ifrw.document.body && !ifrw.document.body.isContentEditable) {
        ifrw.document.body.contentEditable = true;
        ifrw.document.body.contentEditable = false;
    }
}
</script>