javascript - Event ondragstart is no longer triggered in Internet Explorer 9 - Stack Overflow

I've implemented HTML5 drag and drop on DIVs. It works great in all browsers, including IE8. But e

I've implemented HTML5 drag and drop on DIVs. It works great in all browsers, including IE8. But ever since IE9 was released, it not longer works. The ondragstart event does not get triggered. Here is my code, using jQuery:

$('#mydiv')
   .bind('selectstart', function(e) {
        // Prevent text selection
        return false;
    })
    .bind('dragstart', function(e) {
        console.log('dragstart');
    })
    .bind('drag', function(e) {
        console.log('drag');
    })
    .bind('dragend', function(e) {
        console.log('dragend');
    });

and the HTML

<div draggable="true">DnD this thing!</div>

I've implemented HTML5 drag and drop on DIVs. It works great in all browsers, including IE8. But ever since IE9 was released, it not longer works. The ondragstart event does not get triggered. Here is my code, using jQuery:

$('#mydiv')
   .bind('selectstart', function(e) {
        // Prevent text selection
        return false;
    })
    .bind('dragstart', function(e) {
        console.log('dragstart');
    })
    .bind('drag', function(e) {
        console.log('drag');
    })
    .bind('dragend', function(e) {
        console.log('dragend');
    });

and the HTML

<div draggable="true">DnD this thing!</div>
Share Improve this question asked Jul 7, 2011 at 13:35 Martin DrapeauMartin Drapeau 1,53415 silver badges17 bronze badges 1
  • 1 is that a typo ? but your div has no id="mydiv" – Val Commented Jul 7, 2011 at 13:50
Add a ment  | 

1 Answer 1

Reset to default 6

I'm betting that didn't work in IE8, because neither IE8 or IE9 fully support HTML5 drag and drop, that's only been added in IE10 Developer Preview 2. The HTML5 API is based on the implementation of drag and drop in IE5, but there are some differences. Most pertinently, IE9 and before don't support the draggable attribute on elements - the only things that can be dragged in IE9 are things which are draggable by default: text selections, links and images.

So for it to work in IE9 (or IE8) you need to add a link into your HTML (and that link must have an href):

<div id="mydiv"><a draggable="true" href="#">DnD this thing!</a></div>

Your JavaScript should then work as expected with a few slight modifications:

$('#mydiv')
   .bind('selectstart', function(e) {
        // Prevent text selection
        return false;
    })
    .bind('dragstart', function(e) {
        e.originalEvent.dataTransfer.setData("Text", $(e.target).closest('div').attr('id'));
        console.log('dragstart');
    })
    .bind('drag', function(e) {
        console.log('drag');
    })
    .bind('dragend', function(e) {
        console.log('dragend');
    })
    .bind('click', function(e) {
        return false;
    });

Here's an example which I've tested in Firefox and IE9.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744409079a4572783.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信