我正在尝试使用 execCommand 在 iframe 内创建一个表,但它不起作用。它只显示“null”
$(insertTableBtn).click(function() {
iframeDocument.execCommand('insertHTML', false, '<table><tr><td></td></tr></table>');
});
最佳答案
您没有提及您遇到问题的浏览器,也没有可检查的 HTML 代码,因此这里是一个工作版本:
<script src="http://code.jquery.com/jquery-1.12.4.js"></script>
<iframe id="ifr1" src="about:blank">
</iframe>
<button id="btn1">click</button>
<script type="text/javascript">
$(function() {
$('#ifr1').contents().find('html').html('<body contenteditable="true"></body>');
});
$('#btn1').click(function() {
$('#ifr1').contents()[0].execCommand('insertHTML', false, '<table border="1"><tr><td>123</td></tr></table>');
});
</script>
您可以查看jsfiddle:https://jsfiddle.net/3c5Lwmeq/
Note that the
insertHTML
is NOT compatible with Internet Exploere.
关于java - 如何在 iframe jquery execCommand 中创建表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38715253/