2

我使用这个 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>

例如,我需要调用添加 javasctipt,而不是警报:

<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>
4

1 回答 1

0

I suggest you use jquery 1.8.3 (can download here), since version 1.4.2 is depreciated. This is how you should call the function:

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>

Hope this helps and let me know if you have any questions. Happy coding!

于 2012-11-28T04:02:53.193 回答