HTML5 draggable='false' not working in Fir

2019-02-12 10:41发布

I'm simply trying to apply HTML5 draggable='false' attribute to some images but it's not working in Firefox browser. In Chrome working fine but on Firefox, after selecting that image able to drag and drop. Sample code can be seen here:

<div id="dnd">
    <textarea placeholder="drop here"></textarea>
    <img src="http://johnlewis.scene7.com/is/image/JohnLewis/231108668?$prod_main$" draggable='false'/>
</div>

Jsfiddle

I'm having Firefox latest version: 32.0.3

Google a lot but didn't find any better solution. Is there any solution for this without using JavaScript? Any help would be appreciated.

Thanks

3条回答
贪生不怕死
2楼-- · 2019-02-12 11:02

just try this

add ondragstart="return false;" to your html element

<div id="dnd">
    <textarea placeholder="drop here"></textarea>
    <img src="http://johnlewis.scene7.com/is/image/JohnLewis/231108668?$prod_main$" draggable='false' ondragstart="return false;"/>
</div>

查看更多
不美不萌又怎样
3楼-- · 2019-02-12 11:03

You can set the following CSS properties to the image:

.unselectable {
    /* For Opera and <= IE9, we need to add unselectable="on" attribute onto each element */
    /* Check this site for more details: http://help.dottoro.com/lhwdpnva.php */
    -moz-user-select: none; /* These user-select properties are inheritable, used to prevent text selection */
    -webkit-user-select: none;
    -ms-user-select: none; /* From IE10 only */
    user-select: none; /* Not valid CSS yet, as of July 2012 */

    -webkit-user-drag: none; /* Prevents dragging of images/divs etc */
    user-drag: none;
}

HTML code:

<img src="something.jpg" class="unselectable">

Fiddle

查看更多
仙女界的扛把子
4楼-- · 2019-02-12 11:08

Update of sorts, the solution doesn't work with React, however adding the below does.

onDragStart={(e) => { e.preventDefault() }}
查看更多
登录 后发表回答