2009-09-11 3 views
1

PHP는 <p class="select"></p> 태그로 싸여진 ajax를 통해 html 문자열을 html로 보냅니다. css는 클래스를 완벽하게 읽습니다. javascript/jquery는 그렇지 않습니다. javascript/jquery는 심지어 <p onclick="function()"></p>을 구문 분석하지 않습니다. 내가 도대체 ​​뭘 잘못하고있는 겁니까?자바 스크립트 실 거예요 php html 태그를 구문 분석

p.select{color:#fff;padding: 5px;margin:0} 
p.select:hover{color:#fff;background:#f60;cursor:pointer;} 

을 heres 내 스크립트 방법 1 개 JQuery와

을 heres 내 PHP는 (AJAX 미세 통해 데이터를 전송한다)

echo "<p class='select' onclick='populate();' >{$row['song_name']} </p>"; 

은을 heres 내 CSS는 (미세 작동).

$("p .select").click(function(){ 
     var str = $(this).val(); 
     alert(str); 
    }); 

방법 2 onclick 기능.

function populate() 
{ 

alert('here') 
} 

두 가지 방법 중 어느 것도 응답하지 않습니다. 전문가는 왜이일까요? 미래 - - 일치하는 요소

답변

1
$("p .select").live ("click" , function(){ 
     var str = $(this).text(); 
     alert(str); 
    }); 

Events/live

모든 현재의 이벤트 (같은 클릭)에 처리기를 바인딩을 참조하십시오.

+1

는 P 요소가 값 속성을 가지고 있지 않는 한,이 여전히 작동되지는 않지만 : 발()을 아마해야 텍스트를(). http://docs.jquery.com/Attributes/val – NickFitz

0

나는 (Firefox에서) 예제를 게시했습니다. onload 이벤트 안에 jquery 메서드를 넣는 것을 잊었다 고 생각합니다. 다른 (작은) 버그 ...

<html> 
    <head> 
     <style> 
     p.select{color:#fff;padding: 5px;margin:0} 
     p.select:hover{color:#fff;background:#f60;cursor:pointer;} 
     </style>  
     <script src="jquery-1.3.2.min(2).js" type="text/javascript"></script> 
     <script type="text/javascript">   
     function bodyOnLoad() { 
      //instead of "p .select" "p.select" 
      $("p.select").click(
       function(){ 
        //changed val() into text() 
        var str = $(this).text(); 
        alert(str); 
       }); 
     } 
     </script>   
    </head> 
    <body onload="bodyOnLoad()"> 
     <p class='select'>Songname</p> 
    </body> 
</html> 
+0

jQuery는'$ (document) .ready (function() {...});}에 빌드되어 있으며' "' – Eric

1

두 가지 옆에 :

  • p .select는 선택 클래스 요소를 포함 <p> 태그 를 선택합니다. p.select<p class="select">을 선택합니다.
  • populate 기능을 라이브로 이동하지 않는 이유는 무엇입니까? jquery 라이브 (또는 클릭) 의심되는 핸들러를 제거합니다.

이 시도 :

$("p.select").live("click",function() 
{ 
    var str = $(this).text(); 
    alert(str); 
    populate(); 
});