2012-04-05 6 views
0

클릭 된 요소를 알 수 있습니다. 그러나 작동하지 않습니다.어떻게이 코드가

이 코드는 내가 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 
    } 
    } 
})(); 

답변?

+0

가 읽을 수있는 코드 – Raynos

+0

은 읽을 수 없습니다 쓰기 시도? :/ –

+0

눈이 피로해진다. – Raynos

답변

2
(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')); 
    } 
    } 
})(); 
+0

당신은 Chuck Norris입니까? ? 아니면 하느님? 하하, 고마워요 (: –

0

여기에 jquery를 사용할 수 있습니다. 'this'키워드는 클릭 한 현재 요소를 제공합니다. 이 함수는 클래스 이름이 'drop'인 요소의 click 이벤트에서 시작됩니다.

$(".drop").click(function(){ 
    var x=$(this).attr("data-open"); 
}); 
+0

JQuery를 도와 줄 수 있습니까? –

+0

자바 스크립트에서이 작업을 수행하는 방법이 효과가 있을지 100 % 확신 할 수는 없지만 다만 그렇게 사용하는 솔루션을 제공하려고합니다 .. –

+2

위 코드는 jQuery에서도 실행되지 않습니다. POJS'this.getAttribute ('data-open')'또는 jQuery'$ (this) .attr ('data-open')를 사용하십시오. – RobG