<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中使用
我的问题是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>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句