简体   繁体   中英

trigger ctrl+s when button is clicked

I have a button.Now when I click on the button the cltrl+s will be pressed or tiggered.How to do that/ I have tried,

$( "#btn" ).click(function() {
    var e = jQuery.Event( "keydown", { keyCode: 83, ctrlKey:true} );
    jQuery("body").trigger( e );
});

Use an <a href> (html5) like:

<a href="downloads/file.xml" download>Download File</a>

And if you like to create an XML file in javascript and then save it by an a tag you can do:

var xml = '<xml><node1>bla</node1><node2 var="aa">abla</node2></xml>';
xml = btoa(xml); // Converts xml string to base64 (use atob to convert base64 back to string)
var a = document.createElement('a'); // Create A element
a.href = 'data:text/xml;base64,' + xml; // Set base64 encoded href
a.setAttribute('download', ''); // add html5 download tag
a.appendChild(document.createTextNode('download xml file'); // Add link text
body.appendChild(a); // And add element to the page.

Paste this code in a html file and see for your self :)

Note: this code might not work in all browsers, tested with IE11 and Chrome

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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