动态设置图片可拖动

问题描述:

我在设置图片时出现问题可拖动。 此代码似乎并不工作:动态设置图片可拖动

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

我在公共领域库中使用这些函数我正在构建用于闭包编译器的构建。

/** 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}}