REAL ONE eventHandler on multiple items
当我创建列表或网格需要s ame函数我只使用一个事件处理程序 并在处理程序内部决定要执行什么操作。
扩展一个事件处理程序意味着如果您卸载代码,您只需删除一个处理程序,但也使用更少的资源并更新代码更简单。
在你的情况下,我不得不包括一个长函数(切片调用...)来获得点击li 的索引,并检查是否应该执行toggleNav函数。
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<script>
(function(W){
var D;
function init(){
D=W.document;
D.getElementById('ul').addEventListener('click',h,false);
}
function h(e){
var a=e.target,i=Array.prototype.slice.call(a.parentNode.childNodes).indexOf(a);
if(a.parentNode.id=='ul'){
console.log('text: ',a.textContent,' index: ',i,' execute toggleNav: ',i<2);
}
}
W.addEventListener('load',init,false)
})(window)
</script>
</head>
<body>
<ul id="ul"><li>a</li><li>b</li><li>c</li></ul>
</body>
</html>
正如你所看到的代码是短暂的,没有循环。但最重要的是你只有一个处理程序!
这自然适用于现代的浏览器仅但也有一些变化它在所有的浏览器一样,我认为像e=e||window.event; e.target=e.target||e.srcElement
..
编辑
简单的例子,与长变量名和空格随处可见按照matt的要求, 但是使用上面的例子吧缓存各种元素,它与外部库兼容,即使你扩展它。
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<script>
function inizializzation() { // Here i inizialize the eventhandler
var TheULElement=window.document.getElementById('ul'); // search
// for the element which id is ul
TheULElement.addEventListener ('click' , clickhandler , false)
// here i add an event handler to the element which id is ul
}
function clickhandler (clickevent) { // that handler i previously added
if (clickevent.target.parentNode.id == 'ul') { // here i check if the
// parent node id of the element i clicked is ul
console.log(clickevent.target.textContent);
// this writes to the console
//-> rigthclick 'inspect element' (in chrome)
}
}
window.addEventListener ('load' , inizializzation , false)
// this is the event handler when the page loads
// which starts the first function called inizializzation
</script>
</head>
<body>
<ul id="ul">
<li>a</li>
<li>b</li>
<li>c</li>
<li>d</li>
<li>e</li>
<li>f</li>
<li>g</li>
<li>h</li>
<li>i</li>
</ul>
</body>
</html>
这个答案的要点是不只是为了处理2个元素,但多元素
和reflets的titleof问题
如何附上一个单一功能到多元件?
或者还包含多个包含多个元素的元素。
在你需要考虑像大格这种情况下
行是这里面里的我可以添加按钮跨越或什么,并再次只与单一事件处理控制他们所有的李时珍
。
根据行/列号或任何您imggination必须提供。
cols/rows您的处理程序位于parentNode的prantNode上。
if (clickevent.target.parentNode.parentNode.id == 'ul') {
console.log(clickevent.target.textContent);
}
,行/ colums将
<ul> // whatever (div span) it just has to be a element
<li> // whatever ,best if display:block/flex on all thiselements but
<whatever></whatever> // but with WHITESPACES this does not work
<whatever></whatever> // so it's better you add them with a script
<whatever></whatever> // or learn to write without WHITESPACES
<whatever></whatever> // or new lines.
<whatever></whatever> // because a textnode/newline/whitespace
</li> // in dom is a ELEMENT
<li>
<whatever></whatever>
<whatever></whatever>
<whatever></whatever>
<whatever></whatever>
<whatever></whatever>
</li>
</ul>
是否有回线?没有。
ps .:我回答了这个问题,因为它已经是绿旗,所以我只是试图帮助,但我的语言不是英语,所以请随时纠正我的文字。但请不要触摸我的代码。
我想你应该在你的问题中明确指出你想避免冗余!否则,人们会产生这样的印象,即你正在经历一个更基本的问题。 – adarshr
下次我会记得,谢谢。 – peterchon