0

私はこのコードを持っています:

( 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'で要素をクリックしたときに、クリックしていた要素の属性を警告することです。しかし、動作しません。

このコードは、私が彼女のclassNameで要素を選択するために使用するものです。あまり注意を払わないでください。私たちに示すのは、要素の選択方法を示すことだけです。

(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 
    } 
  } 
})();

¿答え?

4

2 に答える 2

1
( 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') );
    }
  }
})();
于 2012-04-05T05:12:01.510 に答える
-1

これにはjqueryを使用できます。ここで、「this」キーワードは、クリックされた現在の要素を提供します。この関数は、クラス名が「drop」である要素のクリック イベントで発生します。

$(".drop").click( function(){
   var x=$(this).attr("data-open");
});
于 2012-04-05T05:11:04.237 に答える