Firing ctrl+r, ctrl+a, ctrl+q events on a button click

别来无恙 提交于 2019-12-12 08:53:49

问题


I need to fire the Ctrl+R, Ctrl+A, Ctrl+Q events when a user clicks on a button.

I was working on the following code:

$(document).ready(function () {
    $('#Button1').click(function () {
        var evt = $.Event("keypress");
        evt.keyCode = 81;
        evt.ctrlKey = true;
        evt.shiftKey = true;
        $(document).trigger(evt);
    });
});

回答1:


You can't simulate browser control keys, but you can simulate their effects.

Ctrl-R refreshes.

function refresh() {
    location.reload(true);
} 

Ctrl-A selects everything. Code is from here.

function selectAll() {
    var e = document.getElementsByTagName('body')[0];
    var r = document.createRange(); r.selectNodeContents(e);
    var s = window.getSelection();
    s.removeAllRanges();
    s.addRange(r);
}

I am not sure what Ctrl-Q is supposed to do; if it quits the browser, that one isn't possible.



来源:https://stackoverflow.com/questions/25763872/firing-ctrlr-ctrla-ctrlq-events-on-a-button-click

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!