简体   繁体   English

Jquery 触发器 function 未准备好文档

[英]Jquery Trigger function not working with document ready

I am working with Php and jquery, I have two buttons for "preview" and "download" content/image But i want whenever i load page(open webpage) then Content/Image should download(trigger should work) Here is my code for "Preview" and "Download"我正在使用 Php 和 jquery,我有两个按钮用于“预览”和“下载”内容/图像但我想每当我加载页面(打开网页)然后内容/图像应该下载(触发器应该工作)这是我的代码“预览”和“下载”

<div id="html-content-holder" style="background-color: #F0F0F1; color: #00cc65; width: 5000px;
        Lorem Ipsum dummy text...
    </div>
    
      <input id="btn-Preview-Image" type="button" value="Preview"/>
    <a id="btn-Convert-Html2Image" href="#">Download</a>
    <br/>
    <h3>Preview :</h3>
    <div id="previewImage">
    </div>

var element = $("#html-content-holder"); // global variable
var getCanvas; // global variable
    $("#btn-Preview-Image").on('click', function () {
         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>

And here i tried so far,Where i am wrong?到目前为止,我在这里尝试过,我错在哪里?

<script>
$("document").ready(function(){
$("#btn-Preview-Image").trigger("click");
setTimeout(function() {
     $("#btn-Convert-Html2Image").trigger("click");
}, 5000);
});
    <script>
/* document ready to load all thing once document ready */
$(document).ready(function(){
    var element = $("#html-content-holder"); // global variable
    var getCanvas; // global variable
    $("#btn-Preview-Image").on('click', function () {
        alert("I m preview");return false;
         /* Your code what you need to do */
    });
    $("#btn-Convert-Html2Image").on('click', function () {
        alert("I m download");return false;
        /* your code to download image */
    });
    /* Trigger event  */  
    $(document).find("#btn-Preview-Image").trigger("click");
    setTimeout(function() {
        $(document).find("#btn-Convert-Html2Image").trigger("click");
    }, 2000);
});
</script>

You have not used find, as if we have to trigger an event in any of the buttons in the whole document we might first need to find that SELECTOR, and then we might perform an action on it.您没有使用 find,好像我们必须在整个文档中的任何按钮中触发一个事件,我们可能首先需要找到该 SELECTOR,然后我们可能会对其执行操作。 The other thing you have not done is you have not kept the events in the ready function.您还没有做的另一件事是您没有将事件保存在准备好的 function 中。

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

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