簡體   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