2013-04-04 29 views
5

所以問題是:你怎麼能做到這一點?我想將draggable屬性更改爲"false",因此之前可拖動的元素將會丟失此屬性。使用javascript訪問`draggable`屬性

我會給你一個div所以你有東西開始:

 <div id="div1" draggable="true" ondragstart="drag(event) "onDblClick="edit('div1')"> 
    </div> 

回答

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

OR

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

如果你需要的屬性完全消失了使用;

elem.removeAttribute('dragable'); 
+0

是的!謝謝你,先生!真的有用! – Tbi45 2013-04-04 09:00:49

3

有很多方法,如果你使用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 
2

你可能想使用jQuery的UI拖動組件acheive UR所需的功能。 。 或者如果你在開發環境中有jQuery庫,你可以使用下面的代碼

$(「#div1」)。attr('draggable',false);

或使用JavaScript,我們可以如下

的document.getElementById( 'DIV1')做的setAttribute( '可拖動', '假')。

0

設置屬性draggabletrue,你大多經常要處理dragstart事件。在原生JS上做到這一點:

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