开发者

Crossbrowser method to cancel a drop event in a content edittable div

开发者 https://www.devze.com 2023-01-26 19:17 出处:网络
I want to be able to disallow images (and other stuff) from being dropped into a content editable div across browsers.I see how to do it for Chrome and IE.Firefox stubbornly refuses.The code snippet a

I want to be able to disallow images (and other stuff) from being dropped into a content editable div across browsers. I see how to do it for Chrome and IE. Firefox stubbornly refuses. The code snippet at http://开发者_如何学Gojsbin.com/oqozo3 has an example which shows that the event is firing, I am cancelling it, and in Firefox the image still appears.

Basically, The following fails to work in Firefox:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>ContentEditable fails in FF</title>
<script type="text/javascript">
function init() {
  var target = document.getElementById("target");
  if (target.addEventListener) {
    target.addEventListener ("dragenter", all, false);
    target.addEventListener ("dragover", all, false);
    target.addEventListener ("drop", all, false);    
    target.addEventListener ("dragdrop", all, false);
  } else {
    target.attachEvent ("ondragenter", all, false);
    target.attachEvent ("ondragover", all, false);
    target.attachEvent ("ondrop", all, false);    
  }
}
function all(event) {
if (event.preventDefault) { event.preventDefault();} 
return false;
}
</script>
</head>
<body onLoad="init();">
    <div id="target" style="border: 1px solid black; height: 100px;" contenteditable="true">This is editable content</div>
    <div>
     <p>I don't want the image to be draggable to the above div. Works in IE,
     Chrome, not firefox.</p>
     <img src="http://mozcom-cdn.mozilla.net/img/firefox-100.jpg">
    </div>
</body>
</html>

I have already looked at: ContentEditable DIV - disabling drag and drop, but the suggestions there also don't work in Firefox.


any reason why you aren't using a framework like jQuery, it does stuff like this that is cross-browser

0

精彩评论

暂无评论...
验证码 换一张
取 消