简体   繁体   English

附加事件循环,javascript

[英]attach event in loop, javascript

I know this is a "classic" and I already tried to read different explanatory articles on this subject, but I still manage to do it wrong somehow. 我知道这是“经典的”,我已经尝试阅读有关该主题的不同说明性文章,但是我仍然设法以某种方式做错了。 I am talking about adding event handlers and functions in a javascript loop. 我说的是在javascript循环中添加事件处理程序和函数。

Here is my code with problems (it's a suggest-box / auto complete) 这是我的有问题的代码(这是一个建议框/自动完成)

function autoCompleteCB(results) {
document.getElementById('autocom').innerHTML = '';
if (results.length == 0) {
    document.getElementById('autocom').style.display = 'none';
} else {
    document.getElementById('autocom').style.display = 'block';
    var divholders = [];
    for (var i = 0; i < results.length; i++) {
        divholders[i] = document.createElement('div');
        divholders[i].style.width = '350px';
        var divrestext = document.createElement('div');
        divrestext.className = 'autocom0';
        divrestext.innerHTML = results[i][0];
        divholders[i].appendChild(divrestext);
        var divrestype = document.createElement('div');
        divrestype.className = 'autocom1' + results[i][1];
        divrestype.innerHTML = results[i][1];
        divholders[i].appendChild(divrestype);
        divholders[i].attachEvent('onmouseover', (function(i) { return function() { divholders[i].style.backgroundColor='#266699'; }; })(i));
        divholders[i].attachEvent('onmouseout', (function (i) { return function() { divholders[i].style.backgroundColor = '#F5F5F5'; }; })(i));
        document.getElementById('autocom').appendChild(divholders[i]);
    }
}
}

It is (of course) the attachevent lines that do not work. (当然)attachevent行不起作用。 This part of javascript is so weird/tricky :) Can a kind expert help me fix those two lines? javascript的这一部分是如此古怪/ tric琐:)一位友善的专家可以帮我解决这两行吗?


This is a half-way fix (I think(: 这是中途解决方法(我认为(:

function bindEvent(element, type, listener) {
if (element.addEventListener) {
    element.addEventListener(type, listener, false);
} else if (element.attachEvent) {
    element.attachEvent('on' + type, listener);
}
}
function autoCompleteCB(results) {
document.getElementById('autocom').innerHTML = '';
if (results.length == 0) {
    document.getElementById('autocom').style.display = 'none';
} else {
    document.getElementById('autocom').style.display = 'block';
    var divholders = [];
    for (var i = 0; i < results.length; i++) {
        divholders[i] = document.createElement('div');
        divholders[i].style.width = '350px';
        var divrestext = document.createElement('div');
        divrestext.className = 'autocom0';
        divrestext.innerHTML = results[i][0];
        divholders[i].appendChild(divrestext);
        var divrestype = document.createElement('div');
        divrestype.className = 'autocom1' + results[i][1];
        divrestype.innerHTML = results[i][1];
        // BIND THE EVENTS
        divholders[i].appendChild(divrestype);
        document.getElementById('autocom').appendChild(divholders[i]);
    }
}
}

It looks like this now, but still no "action" 现在看起来像这样,但是仍然没有“动作”

function autoComplete() {
var ss = document.getElementById('txbkeyword').value;
if (ss.length > 0) { CSearch.SearchAutoComplete(ss, 3, autoCompleteCB); }
else { document.getElementById('autocom').style.display = 'none'; }

}
function bindEvent(element, type, listener) {
if (element.addEventListener) {
    element.addEventListener(type, listener, false);
} else if (element.attachEvent) {
    element.attachEvent('on' + type, listener);
}
}
function autoCompleteCB(results) {
document.getElementById('autocom').innerHTML = '';
if (results.length == 0) {
    document.getElementById('autocom').style.display = 'none';
} else {
    document.getElementById('autocom').style.display = 'block';
    var divholders = [];
    for (var i = 0; i < results.length; i++) {
        divholders[i] = document.createElement('div');
        divholders[i].style.width = '350px';
        var divrestext = document.createElement('div');
        divrestext.className = 'autocom0';
        divrestext.innerHTML = results[i][0];
        divholders[i].appendChild(divrestext);
        var divrestype = document.createElement('div');
        divrestype.className = 'autocom1' + results[i][1];
        divrestype.innerHTML = results[i][1];
        (function (i) {
            bindEvent(divholders[i], 'mouseover', function () {
                divholders[i].style.backgroundColor = '#266699';
            });
            bindEvent(divholders[i], 'mouseout', function () {
                divholders[i].style.backgroundColor = '#F5F5F5';
            });
        })(i);
        divholders[i].appendChild(divrestype);
        document.getElementById('autocom').appendChild(divholders[i]);
    }
}
}

One possibility is because attachEvent is IE-specific. 一种可能性是因为attachEvent是IE特定的。 You'll have to use attachEventListener in many other browsers. 您必须在许多其他浏览器中使用attachEventListener

And, to use the "proper" method for the current browser, you'll need to feature-detect them ( snippet from MDN ): 并且,要对当前的浏览器使用“正确”的方法,您需要对它们进行功能检测( MDN的片段 ):

if (el.addEventListener){
  el.addEventListener('click', modifyText, false);
} else if (el.attachEvent){
  el.attachEvent('onclick', modifyText);
}

You can also create a function to aid in this: 您还可以创建一个函数来辅助此操作:

function bindEvent(element, type, listener) {
    if (element.addEventListener) {
        element.addEventListener(type, listener, false);
    } else if (element.attachEvent) {
        element.attachEvent('on' + type, listener);
    }
}

Then, in place of these 2 lines: 然后,代替这两行:

divholders[i].attachEvent('onmouseover', (function(i) { return function() { divholders[i].style.backgroundColor='#266699'; }; })(i));
divholders[i].attachEvent('onmouseout', (function (i) { return function() { divholders[i].style.backgroundColor = '#F5F5F5'; }; })(i));

...use the function to bind your handlers (skipping the on in the event type argument): ...使用函数绑定您的处理程序(跳过事件类型参数中的on ):

(function (i) {
    bindEvent(divholders[i], 'mouseover', function () {
        divholders[i].style.backgroundColor = '#266699';
    });
    bindEvent(divholders[i], 'mouseout', function () {
        divholders[i].style.backgroundColor = '#F5F5F5';
    });
})(i);

You could also just enclose the <div> : 您也可以只包含<div>

(function (div, i) {
    bindEvent(div, 'mouseover', function () {
        div.style.backgroundColor = '#266699';
    });
    bindEvent(div, 'mouseout', function () {
        div.style.backgroundColor = '#F5F5F5';
    });
})(divholders[i], i);

尝试这个:

divholders[i].attachEvent('onmouseover', this.style.backgroundColor='#266699');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM