2013-10-05 20 views
0

我正在創建一個情緒板創建器,在嘗試將所選圖像的自定義屬性中的值複製到新的div時遇到問題。Jquery拖放在放置事件上獲取自定義屬性值

無論拖放哪個圖像,只要拖放完成,它總是從圖像組中的第一個圖像獲取值。

我創建了一個JSFIDDLE以更好地顯示我的問題。


任何幫助將是偉大的。謝謝:)

下面是HTML的一部分:(在充分的jsfiddle代碼)

<div id="images"> 
<img draggable="true" src="http://i.imgur.com/q9aLMza.png" width="70" height="90"></img> 
<div class="hiddenInfo" data-id="1"></div> 
<div class="hiddenInfo" data-hiddenCrap="blah blah"></div> 
<div class="hiddenInfo" data-hiddenRubbish="bleh bleh"></div> 
</div> 

<div id="images"> 
<img draggable="true" src="http://i.imgur.com/4YgjsPL.jpg" width="70" height="90"></img> 
<div class="hiddenInfo" data-id="2"></div> 
<div class="hiddenInfo" data-hiddenCrap="blah blah"></div> 
<div class="hiddenInfo" data-hiddenRubbish="bleh bleh"></div> 
</div> 

這裏是jQuery代碼的下降部分:(全部代碼是的jsfiddle)
爲了測試起見,我只是試圖用正確的ID創建一個警報。

function handleDrop(e) { 
// this/e.target is current target element. 
    e.preventDefault(); 
     if (e.stopPropagation) { 
      e.stopPropagation(); // stops the browser from redirecting. 
     } 

     var img = document.querySelector('#images img.img_dragging'); 

     console.log('event: ', e); 

     var newImage = new fabric.Image(img, { 
      width: img.width, 
      height: img.height, 
     // Set the center of the new object based on the event coordinates relative 
     // to the canvas container. 
      left: e.layerX, 
       top: e.layerY 
       }); 
      canvas.add(newImage); 

     var dropId = $('.hiddenInfo').data('id'); 
       alert(dropId); 

     var dropId2 = $(this).closest('#images').find('.hiddenInfo').data('id'); 
       alert(dropId2); 


     return false; 
      } 

回答

0

這裏的解決方案:http://jsfiddle.net/wN29y/8/

問題是:

爲#images ID重複 - 改爲類

選擇的元素和屬性更改爲:

var dropId = $(img).next('.hiddenInfo').data('id'); 
      alert(dropId); 
+0

使感謝,感謝您的幫助...我可以有點厚臉皮,再問一個問題上。我將如何選擇data-hiddenRubbish或data-hiddenCrap的數據? – HairlessGoat

+0

沒問題 - 使用:nth-​​child或給他們自己的班級名稱 –

+0

我以爲是這樣....我試過這個http://jsfiddle.net/wN29y/10/但它似乎不工作,任何提示我哪裏錯了? – HairlessGoat