Javascript 使用带解析时间函数声明语法的解盎司函数 我试图从IE Mobile 6输入的表单中删除键盘键值,与IE 3-4的支持率基本一致。p>

Javascript 使用带解析时间函数声明语法的解盎司函数 我试图从IE Mobile 6输入的表单中删除键盘键值,与IE 3-4的支持率基本一致。p>,javascript,Javascript,由于缺乏支持,我无法在声明之后添加事件侦听器,即document.getElementById'elementId'。addEventListener。。。不起作用,我只能内联完成,比如onkeydown=doSomething 是一个jsBin 因此,这个去盎司函数取自: 设置事件功能的建议方法如下: var doSomething = debounce(function() { ... }, 250); 但是,我不能在IE Mobile 6的内联事件侦听器中使用此doSomething样式

由于缺乏支持,我无法在声明之后添加事件侦听器,即document.getElementById'elementId'。addEventListener。。。不起作用,我只能内联完成,比如onkeydown=doSomething

是一个jsBin

因此,这个去盎司函数取自:

设置事件功能的建议方法如下:

var doSomething = debounce(function() { ... }, 250);
但是,我不能在IE Mobile 6的内联事件侦听器中使用此doSomething样式的函数

因此,在标记中,我尝试了:

<input type="text" onkeydown="doSomething()" />
我还试着把去盎司函数的全部内容放在这个函数中,比如:

function doSomething() {
    var timeout, func, wait, immediate;

    func = function() {
        console.log('test');
    };

    wait = 250;

    return function () {
        var context = this, args = arguments;
        var later = function () {
            timeout = null;
            if (!immediate) func.apply(context, args);
        };
        var callNow = immediate && !timeout;
        clearTimeout(timeout);
        timeout = setTimeout(later, wait);
        if (callNow) func.apply(context, args);
    };
}
所以,长话短说:

我怎样才能写出这样一句话:

var doSomething = debounce(...);
像这样:

function doSomething() {

}

你的处境似乎很独特。考虑到您的目标浏览器IE Mobile 6实现了相当古老的技术ECMAScript 3和IE 8中的元素,您将无法使用标准的addEventListener方法。您的部分问题可能是由于使用内联javascript的上下文限制以及您正在针对旧的、旧的1999版JS编程的事实。让我们看看这是怎么回事

编辑 还要确保将代码包装在某种文档中。我将使用jQuery来实现这一点。这可能就是你的JS没有执行的原因

HTML

如果可以使用jQuery:

// make sure the document is ready before trying to access the DOM
$(function() {

    // your debounce function here...

    // jQuery will handle the version compatibility for you
    $('#theInput').keydown(debounce);

});

如果您正在执行var doSomething=debouncefunction{…},250;在函数之外,没有理由不能在onkeydown=doSomething中直接使用它,因此必须进行其他操作。如果在函数中执行此操作,可以尝试window.doSomething=debouncefunction{…},250;要使它全球化,这似乎太过分了。为什么不直接加入oninput事件呢?只是一个提醒@Tom,稍微挖掘一下,发现一个文档说IE Mobile 6只支持ECMAScript 3:[.我正在四处查看,看看这是否与您的document.getElementById和addEventLIstener问题有关……IE在早些年对JS的实现一直有点不稳定。以防您不反对进行一些轻松阅读,这里是ECMAScript 3参考。我检查了deb中的.apply调用是否正确ounce方法是一个问题…看起来不是这样。另外,您使用的确切版本是什么-似乎早期的6.x版本不支持键盘事件
var doSomething = debounce(...);
function doSomething() {

}
<input type='text' id='theInput' />
<script src='yourDebounceScript.js'></script>
// by the time this is executed, 
// DOM elements above this script will be ready
(function() {
    function debounce(func, wait, immediate) {
        var timeout;
        return function () {
            var context = this, args = arguments;
            var later = function () {
                timeout = null;
                if (!immediate) func.apply(context, args);
            };
            var callNow = immediate && !timeout;
            clearTimeout(timeout);
            timeout = setTimeout(later, wait);
            if (callNow) func.apply(context, args);
        };
    }

    var input = document.getElementById('theInput');
    input.attachEvent('keydown' function() {

        // wrap your debounce function in an anonymous function
        // so that you can pass arguments to the debounce as needed
        debounce(function() { /* ... */ }, 250, true);

    });
})();
// make sure the document is ready before trying to access the DOM
$(function() {

    // your debounce function here...

    // jQuery will handle the version compatibility for you
    $('#theInput').keydown(debounce);

});