2016-12-09 43 views
0

我有一個foreach循環內一個div我使用,以顯示視圖內容傳遞動態類名的JavaScript函數

<div class="someclass" data-id="[email protected]"> 
    <p> text1 </p> 
    <p> text2 </p> 
<span class="otherClass embedIcon"></span> 
</div> 

我試圖使用

$(".embedIcon") 
    .click(function() { 
     html2canvas($(this).data('data-id'),{ 
      onrendered: function (canvas) { 
       var myImage = canvas.toDataURL("image/png", 1); 
       window.open(myImage); 
      } 
     }); 
    }); 
爲目標的id屬性

當我點擊應用embedIcon類生成圖像的圖標時,我得到了迭代中所有div的圖像。

如何在單擊div內的圖標時獲取特定div的圖像?

+1

後全部代碼。 – Mahi

+0

它應該是這樣的'html2canvas($(this).data('id')' – Bharat

+0

@Mahi我發佈了完整的代碼 – hello

回答

1

您的ID是你的父母DIV ,而不是在embedIcon,所以你可以使用2個選項

var id = $(this).parent().data('id') 
html2canvas(id, { 

var id = $(this).closest('.someclass').data('id') 
html2canvas(id, { 

更新:

html2canvas接受元素作爲第一個參數,而不是ID,所以你只需要到父傳遞到您的通話html2canvas,你不需要數據-ID。您可以使用.parent().closest(".someclass")

$(".embedIcon") 
    .click(function() { 
     html2canvas($(this).parent(),{ 
      onrendered: function (canvas) { 
       var myImage = canvas.toDataURL("image/png", 1); 
       window.open(myImage); 
      } 
     }); 
    }); 
+0

第二個選項不實現期望的結果 我得到這個錯誤'Uncaught TypeError:node.setAttribute不是函數(...)'當我嘗試第一個選項 – hello

+0

對不起,我忘記了類名之前的點,更新了 – phobia82

+0

我仍然得到同樣的錯誤 – hello

0

如果您正在使用的數據()比它應該是這樣的:

$(this).data('id'); 

,如果使用ATTR()比它應該是這樣的:

$(this).attr('data-id'); 
+0

我仍然得到了和以前一樣的行爲檢查更新的問題和示例代碼 – hello