2013-03-01 73 views
0

我在設置圖片時出現問題可拖動。 此代碼似乎並不工作:動態設置圖片可拖動

var thumb = document.createElement("img"); 
thumb.setAttribute('draggable', "true"); 

創建和我的形象附加到DOM這樣的: VAR拇指=使用document.createElement( 「IMG」);

 thumb.setAttribute('draggable', "true"); 
     thumb.setAttribute('alt', label); 
     thumb.setAttribute('id', "dhmvseries_" + label); 
     thumb.setAttribute("dhmvseriesuuid",label); 

     thumb.ondragstart = thumbDragStart; 
     thumb.ondragend = thumbDragEnd; 
     thumb.onmouseover = displayThumbInfo; 
     thumb.onmouseout = hideThumbInfo; 

var thinner = createElement("div", "dhThumbImage dhRounded"); 
thinner.appendChild(thumb); 
+0

您到目前爲止發現了哪些問題?你會發佈一個jsfiddle http://jsfiddle.net? – 2013-03-01 15:22:00

+1

你是否甚至將元素追加到DOM樹?你是否設置了'src'屬性?這裏需要更多的信息和代碼。做一個小提琴。 _「這段代碼似乎不起作用」_很含糊,你看到了什麼,你期望看到什麼? – 2013-03-01 15:31:39

+0

默認情況下img是可拖動的,嘗試在這裏抓取任何圖像並移動。 – Toping 2013-03-01 17:49:48

回答

0

我在公共領域庫中使用這些函數我正在構建用於閉包編譯器的構建。

/** drag(node n, property p) 
* sets dataTransfer text to value of property p when node n is dragged 
**/ 
function drag(n,p){n['draggable']=1 
    n['ondragstart']=function(a){a.dataTransfer.setData("Text",a.target?a.target[p]:a.srcElement[p])}} 

/** drop(node n, function f) 
* sets a drop event handler for node n that uses function f as its handler 
* function f will be passed: the target node and the dataTransfer "Text" value 
ex. var f=function(t,s){t.style.backgroundColor=s} //sets background the Text value 
* * the "Text" is set to the value of property p in the drag event 
* see drag(node n, property p) 
**/ 
function drop(n,f){n['ondragover']=F 
    n['ondrop']=function(a){f(n,a.dataTransfer.getData("Text")) 
    return!!0}}