jQuery热键调用函数

时间:2012-11-27 23:28:01

标签: jquery hotkeys

我将此jquery https://github.com/tzuryby/jquery.hotkeys用于热键。

请问,我如何称之为“添加”功能:

<script type = "text/javascript" >
    function add(int) {
        if (window.XMLHttpRequest) {
            xmlhttp = new XMLHttpRequest();
        } else {
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                document.getElementById("add").innerHTML = xmlhttp.responseText;
            }
        }
        xmlhttp.open("GET", "method.php?a=" + int, true);
        xmlhttp.send();
    } 
</script>

例如,我需要调用add javasctipt,而不是alert:

<script src="jquery-1.4.2.js"></script>
<script src="jquery.hotkeys.js"></script >

<script> 
    $(document).bind('keydown', 'ctrl + k', function() {
       alert('Teskt hotkey!'); 
       // I need processing javasctipt named "add", not alert
    });
</script>

1 个答案:

答案 0 :(得分:0)

我建议您使用jquery 1.8.3(can download here),因为版本1.4.2已经过折旧。这是你应该如何调用函数:

JAVASCRIPT:

<script src="jquery-1.4.2.js"></script>
<script src="jquery.hotkeys.js"></script>
<script type="text/javascript">
//here you are declaring the function
function add(int) {
    if (window.XMLHttpRequest) {
        xmlhttp = new XMLHttpRequest();
    } else {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            document.getElementById("add").innerHTML = xmlhttp.responseText;
        }
    }
    xmlhttp.open("GET", "method.php?a=" + int, true);
    xmlhttp.send();
} 

$(document).ready(function(){ 
//when you use jquery, 
//it is good practice to wait until the document is ready.
    $(document).bind('keydown', 'ctrl + k', function() {
    //the add function requires an argument, so make sure to provide one.
        add(1);
    });
});
</script>

希望这有帮助,如果您有任何疑问,请与我联系。快乐的编码!