[英]Access the `draggable` attribute with javascript
So the question is : How can you do that ? 所以问题是:你怎么能这样做? I want to change the
draggable
attribute to "false"
so the element that was previously draggable will lose this property. 我想将
draggable
属性更改为"false"
因此先前可拖动的元素将丢失此属性。
I will give you a div so you will have something to start with : 我会给你一个div,所以你将有一些东西可以开始:
<div id="div1" draggable="true" ondragstart="drag(event) "onDblClick="edit('div1')">
</div>
document.getElementById('div1').setAttribute('draggable', false);
OR 要么
var elem = document.getElementById('div1');
elem.setAttribute('draggable', false);
If you want the attribute totally gone use; 如果你想要完全不使用该属性;
elem.removeAttribute('dragable');
There is many ways, if you use jQuery
: 如果使用
jQuery
,有很多方法:
$('#div1').attr('dragabble'); //returns with the value of attribute
$('#div1').attr('dragabble','false'); //sets the attribute to false
Native JS: 原生JS:
document.getElementById('div1').getAttribute('draggable'); //returns the value of attr
document.getElementById('div1').setAttribute('draggable', 'false'); //set the value of attr
You might want to use jquery-ui draggable component to acheive ur required functionality.. or if u have jquery library in ur Dev environment u can use the following code 您可能希望使用jquery-ui draggable组件来实现您所需的功能..或者如果您在Dev环境中有jquery库,您可以使用以下代码
$("#div1").attr('draggable',false);
$( “#DIV1”)ATTR( '拖动',假)。
or using javascript we can do it as follows 或者使用javascript我们可以这样做
document.getElementById('div1').setAttribute('draggable', 'false');
document.getElementById('div1')。setAttribute('draggable','false');
After set the attribute draggable
to true
, you mostly often want to handle dragstart
event. 将属性
draggable
设置为true
,您通常希望处理dragstart
事件。 To do that in native JS: 要在本机JS中执行此操作:
elem.addEventListener('dragstart', ev => {
// dragstart handlings here
}, false)
I'm a little late to the party, but is there a reason why we wouldn't just want to write something like this? 我参加派对有点晚了,但是有没有理由我们不想写这样的东西呢?
var el = document.createElement('div');
el.draggable = false;
Seems to apply the attribute fine for me. 似乎适用于我的属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.