2013-03-30 18 views
0

后,我工作的一个所见即所得的编辑器,并经历与图像的execCommand使用处理一个问题,下面是我的网页药结构的一个简单的例子:图像的execCommand阿贾克斯

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

简化的JavaScript我使用,基本上呈现隐藏的div上传与阿贾克斯的图像,并显示所有上传的图片:

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

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

现在,这一切都工作得很好 - 只要新的图像上载与阿贾克斯我把它添加到图像div:

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

然后,点击任我执行下列函数的结果:

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

现在,这里是其中的execCommand拒绝在Firebug工作,我得到:"getElementById("text_content").document UNDEFIEND"

每隔execCommand函数我运行在该页面上(例如:斜体粗体,字体颜色等),但在这里没有,有人可以帮我找出解决方案吗?

回答

2

获取元素中的document对象的标准方法是通过其contentDocument属性而不是document。这在一些较旧的浏览器中不受支持,但在那些您可以使用contentWindow.document来代替。

因此,下面将在所有的浏览器,除了那些不支持contentDocumentcontentWindow,这在实践中各地今天都没有:

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"); 
    }); 
}