【问题标题】:jQuery hotkeys calling functionjQuery热键调用函数
【发布时间】:2012-11-15 17:10:33
【问题描述】:

我使用这个 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,而不是警报:

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

【问题讨论】:

    标签: jquery hotkeys


    【解决方案1】:

    我建议您使用 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>
    

    希望这会有所帮助,如果您有任何问题,请告诉我。编码愉快!

    【讨论】:

      猜你喜欢
      • 2022-08-03
      • 2018-02-19
      • 2014-06-15
      • 2016-11-25
      • 1970-01-01
      • 2011-12-13
      • 2011-11-07
      • 2012-11-07
      • 1970-01-01
      相关资源
      最近更新 更多