我在 foreach
循环中有一个 div,用于在 View 中显示内容
<div class="someclass" data-id="get_@item.GetYear">
<p> text1 </p>
<p> text2 </p>
<span class="otherClass embedIcon"></span>
</div>
我正在尝试使用
来定位 id 属性$(".embedIcon")
.click(function () {
html2canvas($(this).data('data-id'),{
onrendered: function (canvas) {
var myImage = canvas.toDataURL("image/png", 1);
window.open(myImage);
}
});
});
当我单击应用了 embedIcon
类来生成图像的图标时,我会得到迭代中所有 div 的图像。
当我点击div内的图标时,如何获取特定div的图像?
最佳答案
您的 id 位于您的父 div 上,而不是 embedIcon 上,因此您可以使用 2 个选项
var id = $(this).parent().data('id')
html2canvas(id, {
或
var id = $(this).closest('.someclass').data('id')
html2canvas(id, {
更新:
html2canvas接受元素作为第一个参数,而不是 id,因此您只需将父级传递给 html2canvas 调用,不需要 data-id。您可以使用 .parent()
或 .closest(".someclass")
$(".embedIcon")
.click(function () {
html2canvas($(this).parent(),{
onrendered: function (canvas) {
var myImage = canvas.toDataURL("image/png", 1);
window.open(myImage);
}
});
});
关于javascript - 将动态类名传递给 JavaScript 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41053761/