2014-05-22 5 views
-2

중요한주의 사항 사람들이 잡히지 않아 (내가 한 것처럼) : 이것은 jQuery처럼 보일 수 있습니다. 이지만이 아닙니다.
솔직히 말해서 나는 더 잘 알려야했습니다. 나는 jQuery 이외의 것을 $으로 사용한다. 오 잘. 수업은 배웠다!함수를 실행할 수 없습니다.

(HTML) :

<html> 
    <head> 
     <script src="selector.js"></script> 
    </head> 
    <body> 
     <label id="id">A label</label> 
     <script> 
     $("label #id").clicked(function(){ 
      alert("ASDASD"); 
     }); 
     </script> 
    </body> 
</html> 

(JS) : 내가 레이블을 클릭하면

/* 
    NAME : SELECTOR.JS 
*/ 

function $(attr){ 

    // Removed space in front of the variable 
    while(attr.charAt(0) == " "){ 
     attr = attr.substr(1); 
     if(attr == ""){ 
      return 0; 
     } 
    } 

    // Completed the query 
    if(attr.length > 1){ 
     return Array.prototype.slice.call(document.querySelectorAll(attr)); 
    }else{ 
     if(attr.length == 1){ 
      return new Object(document.querySelector(attr)); 
     }else{ 
      return null; 
     } 
    } 
} 

Object.prototype.clicked = function(script){ 
    if(typeof(this) == "object"){ 
     if(this.constructor == Array){ 
      for(var i = 0; i < this.length; i++){ 
       this[i].onclick = script; 
      } 
     }else{ 
      if(this.constructor == Object){ 
       console.log("SINGLE OBJECT : " + this); 
       console.log("SINGLE OBJECT : ONCLICK : " + this); 
       this.onclick = script; 
      }else{ 
       console.log("ERROR : this.constructor is not 'Object' or 'Array'."); 
       return null; 
      } 
     } 
    }else{ 
     console.log("ERROR : typeof(this) is not 'Object'."); 
     return null; 
    } 

    return this; 
}; 

, 내가 얻을 수없는 경고 상자 다크 Absol

코드 ~ NIET 본. 어떻게해야합니까? js 파일의 파일 이름은 selector.js입니다. 나는 달릴 기능이 필요합니다. Pls 도움!

나는 이것이 최소화 된 코드라고 생각합니다.

+1

을'id'은 * 그는'

+0

gshhh .... downvoters 오늘 바쁘다 :-) –

+0

Downvoters 내 정확한 대답에 투표했다. Argh –

답변

1

공간이 잘못되어 $("label #id") 대신 $("label#id")이 필요합니다. label#id 사이의 공백을 사용하면 label의 요소를 id="id"으로 검색하고 labelid="id"으로 검색 할 공간이 없습니다.

+2

사실, "$"("# id")'가 필요합니다. ID는 고유하며 더 이상 자격을 부여하는 것은 불필요합니다. –

+0

이것이 내가 기대하는 바입니다. 고맙습니다! –

-1

기능은 사용

.clicked() 

은 유효한 jQuery를 기능하지 않습니다. 당신이 찾고있는 하나입니다 :

$("#target").click(function() { 
     alert("Handler for .click() called."); 
}); 

여기를 참조하십시오 : 귀하의 선택이 ID를 가진 요소 "를 찾고 있습니다

http://api.jquery.com/click/

관련 문제