我有这个代码:
( function() {
var i, ii, e = Elements.Select('.drop');
for ( i = 0, ii = e.length; i < ii; i++ ) {
e [ i ].onclick = function () {
alert ( e [ i ].getAttribute('data-open') );
}
}
})();
我所做的是,当单击 className 为“drop”的元素时,然后提醒我正在单击的元素的属性。但不起作用。
这段代码是我用来通过她的类名选择一个元素的代码。 不要太注意,它只是向我们展示只是向他们展示我如何选择元素。
(function() {
Select : function ( element ) {
var object, index = element.substr( 0, 1 ), name = element.substr( 1, element.length ), clases = [ ], i, all = document.body.getElementsByTagName("*");
switch ( index ) {
case '.' :
for ( i = 0; i < all.length; i ++ ) {
if ( all [ i ].className == name ) {
clases.push( all [ i ] );
}
}
object = clases;
break;
return object
}
}
})();
请问答案?
最佳答案
( function() {
var i, ii, e = Elements.Select('.drop');
for ( i = 0, ii = e.length; i < ii; i++ ) {
e [ i ].onclick = function () {
//by the time that this gets executed, the for loop is ended, thus i equals ii
// instead of using e[i]... try using this :
alert (this.getAttribute('data-open'));
alert ( e [ i ].getAttribute('data-open') );
}
}
})();
关于javascript - 如何知道哪个元素被点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10022934/