gpt4 book ai didi

javascript - 将 anchor id 和按钮 id 传递给 jquery

转载 作者:行者123 更新时间:2023-12-03 05:25:32 25 4
gpt4 key购买 nike

我有这段代码,它对于一个 block 来说效果很好,并将 div 元素保存为图像。我正在使用 html5 Canvas 。如果我有 n 个具有唯一 id 的此类 div,我如何实现将 id 和其他变量传递给 jquery。

下面是我的代码:-

<body>
<div class="html-content-holder" style="background-color: #F0F0F1; color: #00cc65; width: 500px;
padding-left: 25px; padding-top: 10px;">
<strong>Codepedia.info</strong><hr/>
<h3 style="color: #3e4b51;">
Html to canvas, and canvas to proper image
</h3>

</div>
<input class="btn-Preview-Image" type="button" value="Preview"/>
<a class="btn-Convert-Html2Image" href="#">Download</a>
<br/>
<h3>Preview :</h3>
<div class="previewImage">
</div>

<div class="html-content-holder" style="background-color: #F0F0F1; color: #00cc65; width: 500px;
padding-left: 25px; padding-top: 10px;">
<strong>Codepedia.info</strong><hr/>
<h3 style="color: #3e4b51;">
Html to canvas, and canvas to proper image
</h3>

</div>
<input class="btn-Preview-Image" type="button" value="Preview"/>
<a class="btn-Convert-Html2Image" href="#">Download</a>
<br/>
<h3>Preview :</h3>
<div class="previewImage">
</div><div class="html-content-holder" style="background-color: #F0F0F1; color: #00cc65; width: 500px;
padding-left: 25px; padding-top: 10px;">
<strong>Codepedia.info</strong><hr/>
<h3 style="color: #3e4b51;">
Html to canvas, and canvas to proper image
</h3>

</div>
<input class="btn-Preview-Image" type="button" value="Preview"/>
<a class="btn-Convert-Html2Image" href="#">Download</a>
<br/>
<h3>Preview :</h3>
<div class="previewImage">
</div><div class="html-content-holder" style="background-color: #F0F0F1; color: #00cc65; width: 500px;
padding-left: 25px; padding-top: 10px;">
<strong>Codepedia.info</strong><hr/>
<h3 style="color: #3e4b51;">
Html to canvas, and canvas to proper image
</h3>

</div>
<input class="btn-Preview-Image" type="button" value="Preview"/>
<a class="btn-Convert-Html2Image" href="#">Download</a>
<br/>
<h3>Preview :</h3>
<div class="previewImage">
</div><div class="html-content-holder" style="background-color: #F0F0F1; color: #00cc65; width: 500px;
padding-left: 25px; padding-top: 10px;">
<strong>Codepedia.info</strong><hr/>
<h3 style="color: #3e4b51;">
Html to canvas, and canvas to proper image
</h3>

</div>
<input class="btn-Preview-Image" type="button" value="Preview"/>
<a class="btn-Convert-Html2Image" href="#">Download</a>
<br/>
<h3>Preview :</h3>
<div class="previewImage">
</div><div class="html-content-holder" style="background-color: #F0F0F1; color: #00cc65; width: 500px;
padding-left: 25px; padding-top: 10px;">
<strong>Codepedia.info</strong><hr/>
<h3 style="color: #3e4b51;">
Html to canvas, and canvas to proper image
</h3>

</div>
<input class="btn-Preview-Image" type="button" value="Preview"/>
<a class="btn-Convert-Html2Image" href="#">Download</a>
<br/>
<h3>Preview :</h3>
<div class="previewImage">
</div><div class="html-content-holder" style="background-color: #F0F0F1; color: #00cc65; width: 500px;
padding-left: 25px; padding-top: 10px;">
<strong>Codepedia.info</strong><hr/>
<h3 style="color: #3e4b51;">
Html to canvas, and canvas to proper image
</h3>

</div>
<input class="btn-Preview-Image" type="button" value="Preview"/>
<a class="btn-Convert-Html2Image" href="#">Download</a>
<br/>
<h3>Preview :</h3>
<div class="previewImage">
</div><div class="html-content-holder" style="background-color: #F0F0F1; color: #00cc65; width: 500px;
padding-left: 25px; padding-top: 10px;">
<strong>Codepedia.info</strong><hr/>
<h3 style="color: #3e4b51;">
Html to canvas, and canvas to proper image
</h3>

</div>
<input class="btn-Preview-Image" type="button" value="Preview"/>
<a class="btn-Convert-Html2Image" href="#">Download</a>
<br/>
<h3>Preview :</h3>
<div class="previewImage">
</div><div class="html-content-holder" style="background-color: #F0F0F1; color: #00cc65; width: 500px;
padding-left: 25px; padding-top: 10px;">
<strong>Codepedia.info</strong><hr/>
<h3 style="color: #3e4b51;">
Html to canvas, and canvas to proper image
</h3>

</div>
<input class="btn-Preview-Image" type="button" value="Preview"/>
<a class="btn-Convert-Html2Image" href="#">Download</a>
<br/>
<h3>Preview :</h3>
<div class="previewImage">
</div>


<div class="html-content-holder" style="background-color: #F0F0F1; color: #00cc65; width: 500px;
padding-left: 25px; padding-top: 10px;">
<strong>Codepedia.info</strong><hr/>
<h3 style="color: #3e4b51;">
Html to canvas, and canvas to proper image
</h3>

</div>
<input class="btn-Preview-Image" type="button" value="Preview"/>
<a class="btn-Convert-Html2Image" href="#">Download</a>
<br/>
<h3>Preview :</h3>
<div class="previewImage">
</div>



<script>
$(document).ready(function(){
var getCanvas; // global variable
$(".btn-Preview-Image").on('click', function () {
element=$('.html-content-holder');
// getCanvas;
preview= element.find(".previewImage");
html2canvas(element, {
onrendered: function (canvas) {
$(".previewImage").append(canvas);
getCanvas = canvas;
}
});
});

$(".btn-Convert-Html2Image").on('click', function () {
var imgageData = getCanvas.toDataURL("image/png");
var newData = imgageData.replace(/^data:image\/png/, "data:application/octet-stream");
$(".btn-Convert-Html2Image").attr("download", "your_pic_name.png").attr("href", newData);
});

});

</script>
</body>

最佳答案

标记:你必须为每个重复的组添加一个父div并将ID更改为class,然后使用find进行查询。

    <div class="block-holder">
<div class="html-content-holder" style="">
<strong>Codepedia.info</strong>
<hr/>
<h3 style="color: #3e4b51;">
Html to canvas, and canvas to proper image
</h3>

</div>
<input class="btn-Preview-Image" type="button" value="Preview" />
<a class="btn-Convert-Html2Image" href="#">Download</a>
<br/>
<h3>Preview :</h3>
<div class="previewImage">
</div>
</div>

脚本

$(document).ready(function() {
$(".btn-Preview-Image").on('click', function() {
parent = $(this).closest('.block-holder');
element=parent.find('.html-content-holder')
// getCanvas;
console.log(parent,element);
preview = element.find(".previewImage");
html2canvas(element, {
onrendered: function(canvas) {
$(".previewImage").html(canvas);

}
});
});

$(".btn-Convert-Html2Image").on('click', function() {
parent = $(this).closest('.block-holder');
getCanvas = parent.find('canvas')[0];
console.log(parent,getCanvas)
var imgageData = getCanvas.toDataURL("image/png");
var newData = imgageData.replace(/^data:image\/png/, "data:application/octet-stream");
$(".btn-Convert-Html2Image").attr("download", "your_pic_name.png").attr("href", newData);
});

});

您可以看到 preview= element.find(".previewImage"),对 getcanvas 变量和其他变量使用相同的技术。

这里正在运行 fiddle https://jsfiddle.net/5cfz2ufp/10/

关于javascript - 将 anchor id 和按钮 id 传递给 jquery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41156878/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com