weixin_33744141 2013-03-30 16:09 采纳率: 0%
浏览 33

ajax之后的图像execCommand

I'm working on a WYSIWYG editor and experiencing a problem with the image handling using execCommand, the following is a simplified example of my page structue:

<div id="buttons_panel"><input id="img_submit" type="button"/></div>

<div id="img_handle" style="display:none;">
<div id="ajax_upload"></div> /* AJAX IMG UPLOAD FROM */
<div id="images"></div> /* DIV FOR ALL UPLOADED IMAGES DISPLAY */
</div>

<iframe id="text_content"></iframe>

the simplified JavaScript I'm using, basically showing the hidden div uploading an image with ajax and displaying all uploaded images:

<script>
$("#img_submit").click(function(){
$("#img_handle").show();

/* HANDLE IMG UPLOAD WITH AJAX AND RELOAD ALL IMAGES INTO #images DIV */
});
</script>

Now, all of this works just fine - as soon as the new image is uploaded with ajax I append it to the images div:

function loadImgs(){
var loadImages="PATH/TO/URL"; 
$.post(loadImages, {request:"loadImages"}, function(response){
$("#images").append(response);
insert_img();
});
}

Then, on click on either of the results i run the following function:

function insert_img(){$(".img_insert").click(function(){
var frame = document.getElementById('text_content'); frame.document.execCommand('InsertImage',false,"../PATH/TO/IMG"); 
});}

Now, here is where the execCommand refuses to work in firebug i get: "getElementById("text_content").document UNDEFIEND"

Every other execCommand functions I run on that page (ex: italic Bold, font-color etc..) works, but here it doesn't, can some one please help me figure out a solution?

  • 写回答

1条回答 默认 最新

  • weixin_33724659 2013-03-30 16:57
    关注

    The standard way to get hold of the document object within an <iframe> element is via its contentDocument property rather than document. This isn't supported in some older browsers but in those you can use contentWindow.document instead.

    So, the following will work in all browsers except those which do not support contentDocument or contentWindow, which in practice are not around today:

    function getIframeDocument(iframeEl) {
        return iframeEl.contentDocument || iframeEl.contentWindow.document;
    }
    
    function insert_img(){
        $(".img_insert").click(function() {
            var frame = document.getElementById('text_content');
            getIframeDocument(frame).execCommand('InsertImage',false,"../PATH/TO/IMG");
        });
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办