2012-09-12 2 views
0

URL에 #을 통해 전달 된 값이 있습니다.이 클래스의 이름은 jQuery를 사용하여 해당 확인란 ID의 값을 가져 오는 것입니다.jQuery는 알려진 동적 클래스 이름에서 ID 이름을 가져옵니다.

// get hashtag value 
if(self.document.location.hash){ 
var urlhash = self.document.location.hash; 
var hashtagval = self.document.location.hash.replace("#",""); 

// get id of element from hashtagval (class name) 
var counter = $('.'+hashtagval).attr('id'); 
} 
+1

무엇이 문제입니까? – Stefan

+1

자, 무엇이 당신의 질문입니까? 그 옆에'var hashtagval = urlhash.replace ("#", "");' – alexbusu

+0

함수를'$ (document) .ready (function() {/*...*/})', 그 요소는 다른 목적으로는 존재하지 않을 것이기 때문에 ... 그러나 당신은 문제가 정확히 무엇인지 더 자세히 설명해야합니다. – nbrooks

답변

0

좋아요, 테스트 해봤는데 #a와 #b를 해시에 추가하면 문제가 없습니다.

해시 속성을 남용하고 있습니다. 해시 속성은 페이지의 viewstate (예 : 화면이이 단락으로 이동해야 함)를 정의하는 데 사용되며 일부 로컬 변수는 저장하지 않고 응용 프로그램 논리에 사용됩니다.

<!doctype html> 
    <html> 
     <head> 
      <meta charset="utf-8"> 
      <title>Demo</title> 
      <script src="jquery.js"></script> 
      <script> 
       $(document).ready(function() { 
        var change = true; 
        $('#ba').click(function() { 
         // get hashtag value 
         if(self.document.location.hash) { 
          var urlhash = self.document.location.hash; 
          var hashtagval = self.document.location.hash.replace("#", ""); 

          // get id of element from hashtagval (class name) 
          var id = $('.' + hashtagval).attr('id'); 
          $('body').append('<h1>id=' + id); 
         } 
        }); 
       }); 
      </script> 
     </head> 
     <body> 

      <button type="button" id="ba" > 
       Click Me! 
      </button> 

      <div> 
       <p id="foundthisthing" class="a"> 
        Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. 
       </p> 
       <p id="thisworkstoo"class="b"> 
        eafdsaf dsa dgsf dgadg dfg dagfadgLorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. 
       </p> 
      </div> 
     </body> 
    </html> 
+0

ha 예 그것은 클래스를 urlencoded했기 때문에 작동합니다. 이름 –

관련 문제