douao1959 2017-05-01 06:26
浏览 293

Ajax锚点链接触发器不起作用

I am using html2canvas javascript to convert html div to image file. All script are working good and it converts html div to image properly.

But when I am adding response(anchor link) to href and triggering it using ajax it is not triggering anchor link.

Here is my code,

Html code:

<div id="mainDiv">

<h1>Heading H1</h1>
<p>Download Image using ajax and php</p>

</div>

<div>
<a id="download">Download Image</a><br/>
<a id="download-image" href="" download>Download</a>
</div>

Ajax code

<script>

    $('#download').on('click', function () {
html2canvas([document.getElementById('mainDiv')], {
    onrendered: function (canvas) {
        var imagedata = canvas.toDataURL('image/png');
        var imgdata = imagedata.replace(/^data:image\/(png|jpg);base64,/, "");
        //ajax call to save image inside folder
        $.ajax({
            url: 'save_image.php',
            data: {
                   imgdata:imgdata
                   },
            type: 'post',
            success: function (response) {   
               $('#download-image').attr('href', response).trigger('click');
               //$('#downlaod-image').trigger('click');
            }
        });
    }
});
});

</script>
  • 写回答

1条回答 默认 最新

  • dpd3447 2017-05-01 06:31
    关注

    Clicking the link will actually reload your page. It will still execute html2canvas(), but the response from the Ajax call will never be processed, as by that time the page has already been reloaded.

    So cancel the default hyperlink action by adding a return false just before the end of the click handler (after the call to html2canvas).

    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!