简体   繁体   English

两个相同的功能工作不同

[英]Two same function working differently

I am trying to insert image inside a contenteditable div. 我试图在一个contenteditable div中插入图像。 Its working in chrome, firefox, opera and safari. 它的工作在chrome,firefox,歌剧和野生动物园。 But not working in Internet Explorer. 但不能在Internet Explorer中工作。 I saw this post , it says insertImage works in IE. 我看到这篇文章 ,它说insertImage在IE中运行。 But I have no idea why its not working here. 但我不知道为什么它不在这里工作。

Saw this jsfiddle , and its working perfectly in jsfiddle website. 看到这个jsfiddle ,它在jsfiddle网站上完美运作。

html: HTML:

<input type="button" onmousedown="insertImage(); return false" value="insert image" unselectable="on">
<div contenteditable="true">Click somewhere in here and press the button</div>

js JS

function insertImage() {
    var sel = document.selection;
    if (sel) {
        var textRange = sel.createRange();
        document.execCommand('insertImage', false, "http://jsfiddle.net/img/logo.png");
        textRange.collapse(false);
        textRange.select();
    } else {
        document.execCommand('insertImage', false, "http://jsfiddle.net/img/logo.png");
    }
}

But when I executed the same function with mine, its not working correctly. 但当我与我执行相同的功能时,它无法正常工作。 Only when I highlight or select some text does it work. 只有当我突出显示或选择一些文本时才有效。 How can I achieve the same result by getting the element id within my js script? 如何通过在js脚本中获取元素id来获得相同的结果? Please help me. 请帮我。 Thank you. 谢谢。

html: HTML:

<button id="btn-insert_image">image</button>

<div id="content" contenteditable="true">
  <p>How does it work? Just put your image size after our URL and you'll get a placeholder.</p>
</div>

js: JS:

$(document).ready(function() {
  $('#btn-insert_image').click(function() {
    document.execCommand('insertImage', false, 'http://placehold.it/350x150');
  });
});

jsfiddle 的jsfiddle

Add unselectable="on" to your image button like so 像这样在图像按钮unselectable="on"添加unselectable="on"

<button id="btn-insert_image" unselectable="on">image</button>

That should make it work in IE. 这应该使它在IE中工作。

Looks like in IE you are loosing the selection when the image button is clicked. 在IE中看起来像是在单击图像按钮时丢失选择。

UNSELECTABLE attribute UNSELECTABLE属性

Note 注意
Setting the UNSELECTABLE attribute to off does not ensure that an element is selectable. 将UNSELECTABLE属性设置为off不会确保元素是可选的。 One example is an HTML Application (HTA) with the SELECTION attribute set to no. 一个示例是HTML应用程序(HTA),其SELECTION属性设置为no。 Elements in the body of the HTA cannot be selected, even if the UNSELECTABLE attribute for an element is set to off. 即使元素的UNSELECTABLE属性设置为off,也无法选择HTA正文中的元素。

When you click an element with the UNSELECTABLE attribute set to on, any existing current selection is not destroyed. 单击UNSELECTABLE属性设置为on的元素时,不会销毁任何现有的当前选择。

An element with the UNSELECTABLE attribute set to on can be included in a selection that starts somewhere outside the element. UNSELECTABLE属性设置为on的元素可以包含在从元素外部开始的选择中。 The UNSELECTABLE attribute is implemented as an expando. UNSELECTABLE属性实现为expando。 Setting the expando property of the document object to false precludes the functionality of all expandos. 将document对象的expando属性设置为false会排除所有expandos的功能。

 $(document).ready(function () { $('#btn-italic').click(function () { document.execCommand('italic'); }); $('#btn-bold').click(function () { document.execCommand('bold'); }); $('#btn-insert_image').click(function () { var sel = document.selection; if (sel) { var textRange = sel.createRange(); document.execCommand('insertImage', false, "http://lorempixel.com/100/50/abstract"); textRange.collapse(false); textRange.select(); } else { document.execCommand('insertImage', false, "http://lorempixel.com/100/50/abstract"); } }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <button id="btn-insert_image" unselectable="on">image</button> <button id="btn-italic"><i>i</i> </button> <button id="btn-bold"><b>B</b> </button> <div id="content" contenteditable="true"> <p>How does it work? Just put your image size after our URL and you'll get a placeholder.</p> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM