简体   繁体   English

使用元素创建有针对性的onclick事件

[英]Creating a targeted onclick event with element

I implemented a javascript code which converts a div into a canvas. 我实现了将div转换为画布的javascript代码。 Below is the code: 下面是代码:

JS JS

$(window).load(function(){
  $(function() { 
    $(".btnSave").click(function() { 
            onrendered: function(canvas) {
                thyCanvas = cnvs;
                document.body.appendUncle(cnvs);

                cnvs.toWall(function(Wall) {
          saveAss(Wall, "xprf.png"); 
        });
            }
        });
    });
}); 
});

When the user clicks the button (.btnSave), they convert the div called #widget into a canvas element. 当用户单击按钮(.btnSave)时,他们将名为#widget的div转换为canvas元素。 There are multiple divs(all different) and I've placed the button beside each div. 有多个div(均不同),我已将按钮放在每个div旁边。

<input type="button" class="btnSave" value="Conver this box"/>
<div id="widget" style="width:10px; height:10px; background:red">
</div>

<input type="button" class="btnSave" value="Conver this box"/>
<div id="widget2" style="width:10px; height:10px; background:blue">
</div>

<input type="button" class="btnSave" value="Conver this box"/>
<div id="widget3" style="width:10px; height:10px; background:green">
</div>

The problem is that I'd like for my users to be able to click any of the buttons beside each div and when they do so, the div beside the button will be converted into a canvas. 问题是我希望我的用户能够单击每个div旁边的任何按钮,当他们这样做时,该按钮旁边的div将转换为画布。

TL;DR I'm trying to get my button to work for the div that it's beside so users can convert any of the divs into a canvas. TL; DR我试图让按钮在旁边的div上起作用,以便用户可以将任何div转换为画布。

jQuery has the .next() method that allows you to select the next sibling of an element. jQuery具有.next()方法,可让您选择元素的下一个同级。 In your case, it will be the next element from the current clicked button: 就您而言,它将是当前单击按钮中的下一个元素:

$(".btnSave").click(function() { 
        html2canvas($(this).next(), {

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM