[英]how to grab div with html2canvas?
I'm trying to use html2canvas. 我正在尝试使用html2canvas。 I need to grab a specific div and add it to the body.
我需要抓取一个特定的div并将其添加到正文中。
The code from official site works fine as a demonstration. 来自官方站点的代码可以很好地用作演示。 But When I try to select my div, nothing happens.
但是,当我尝试选择div时,什么也没有发生。
Here's what's working 这是工作原理
html2canvas(document.body, {
onrendered: function(canvas) {
document.body.appendChild(canvas);
},
width: 500,
height: 500
});
and here's what's not working 这是不起作用的
html2canvas($("#div_name"), {
onrendered: function(canvas) {
document.body.appendChild(canvas);
},
width: 500,
height: 500
});
what am I doing wrong? 我究竟做错了什么? ty
ty
up 上
<div id="dsf" class="sdf">
<%= render "share_form" %>
</div>
up 上
<script src="https://cdnjs.cloudflare.com/ajax/libs/html2canvas/0.4.1/html2canvas.min.js"></script>
<script type="text/javascript">
html2canvas($("#dsf"), {
onrendered: function(canvas) {
document.body.appendChild(canvas);
}
});
</script>
if I use document.body
it works both with or withour $( document ).ready(function() {
如果我使用
document.body
,它既可以与$( document ).ready(function() {
try 尝试
html2canvas(document.getElementById("div_name"), { onrendered: function(canvas) { document.body.appendChild(canvas); }, width: 500, height: 500 });
and make sure you have a div with an id of div_name 并确保您有一个ID为div_name的div
You have to execute the script when the document is loaded. 加载文档时必须执行脚本。 So you have to replace
所以你必须更换
<script type="text/javascript">
html2canvas($("#dsf"), {
onrendered: function(canvas) {
document.body.appendChild(canvas);
}
});
</script>
with 与
<script type="text/javascript">
$(document).ready(function () {
html2canvas($("#dsf"), {
onrendered: function(canvas) {
document.body.appendChild(canvas);
}
});
});
</script>
(For more information see http://learn.jquery.com/using-jquery-core/document-ready/ ) (有关更多信息,请参见http://learn.jquery.com/using-jquery-core/document-ready/ )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.