繁体   English   中英

使用javascript访问`draggable`属性

[英]Access the `draggable` attribute with javascript

所以问题是:你怎么能这样做? 我想将draggable属性更改为"false"因此先前可拖动的元素将丢失此属性。

我会给你一个div,所以你将有一些东西可以开始:

     <div id="div1"  draggable="true" ondragstart="drag(event) "onDblClick="edit('div1')">
     </div>
document.getElementById('div1').setAttribute('draggable', false);

要么

var elem = document.getElementById('div1');
elem.setAttribute('draggable', false);

如果你想要完全不使用该属性;

elem.removeAttribute('dragable');

如果使用jQuery ,有很多方法:

    $('#div1').attr('dragabble'); //returns with the value of attribute
    $('#div1').attr('dragabble','false'); //sets the attribute to false

原生JS:

    document.getElementById('div1').getAttribute('draggable'); //returns the value of attr
    document.getElementById('div1').setAttribute('draggable', 'false'); //set the value of attr

您可能希望使用jquery-ui draggable组件来实现您所需的功能..或者如果您在Dev环境中有jquery库,您可以使用以下代码

$( “#DIV1”)ATTR( '拖动',假)。

或者使用javascript我们可以这样做

document.getElementById('div1')。setAttribute('draggable','false');

将属性draggable设置为true ,您通常希望处理dragstart事件。 要在本机JS中执行此操作:

elem.addEventListener('dragstart', ev => {
  // dragstart handlings here
  }, false)

我参加派对有点晚了,但是有没有理由我们不想写这样的东西呢?

var el = document.createElement('div');
el.draggable = false;

似乎适用于我的属性。

暂无
暂无

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

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