2012-09-03 2 views
0

이 코드는 반복됩니다. 나는이 작업을 얻기 위해 모든 노력을 한Jquery를 사용하여 페이지의 여러 반복 클래스에서 특정 요소 가져 오기

function upvote(box){ 
$(this).siblings('.upvoteimage').load('upvote.php?box=' + box); 
} 

:

<div class='commentfloatleft'> 
       <button type='button' class='button2' onclick='upvote($id8)'> 
        <img src='images/likebutton.png' class='upvoteimage' width='12' height='12' alt='PearlSquirrel'/> 
       </button> 
       <button type='button' class='button2' onclick='downvote($id8)'> 
        <img src='images/unlikebutton.png' width='12' height='12' alt='PearlSquirrel'/> 
       </button> 
      </div> 

내가 upvote에 대한 온 클릭 코드를 클릭

이있다. get 함수가 작동하지만 ".upvoteimage"에서 변경 사항을 볼 수 없습니다. 누구든지이 문제를 해결하는 방법을 알고 있습니다. 도움말 크게 감사하겠습니다. 감사.

+0

버튼들이'

' – ClydeFrog

+0

사실에 의해 포위 될 필요가 해고하기 위해, 그건 그냥 양식을 해고 할 수있다. –

+0

그는 이미 가지고있다. (인라인, 모범 사례는 아니지만 작동한다.) –

답변

2

이 방법으로 시도해보십시오 img 태그, 그 빈 태그에 아무것도로드 어차피 : 당신의 "버튼"요소에 클래스 "upvote에"로 설정 :

<div class='commentfloatleft'> 
       <button type='button' class='button2 upvote' id='$id8'> 
        <img src='images/likebutton.png' class='upvoteimage' width='12' height='12' alt='PearlSquirrel'/> 
       </button> 
       <button type='button' class='button2' onclick='downvote()'> 
        <img src='images/unlikebutton.png' width='12' height='12' alt='PearlSquirrel'/> 
       </button> 
      </div>​ 

을 그리고 사용이 코드를 JS :

$(".upvote").on("click",function(){ 
    var box=$(this).attr("id"); 
    $(this).find('.upvoteimage').attr("src",'upvote.php?box=' + box); 
    return false; 
}​);​ 
+0

이것은 완벽하게 작동했습니다. 늦은 응답에 대해 죄송합니다. – Eggo

+0

당신은 환영합니다 :) – khomyakoshka

2

코드에 형제가 표시되지만 .upvoteimage는 형제가 아니라 아이입니다. 대신 find/children을 사용해보십시오.

그리고 $ (this)는 아마도 함수에 존재하지 않습니다. 어쩌면 스크립트 블록으로 onclick 이벤트를 이동해야합니다. jQuery에는 인라인 코드를 사용하지 않는 몇 가지 훌륭한 기능이 있습니다.

+0

아직도 작동하지 않는 것 같습니다. – Eggo

+0

Ashirvad Singh이 전체 패키지로 대답했습니다 (아직 설명하지 않았습니다). –

0

버튼의 형제를 참조하고 클래스 upvoteimage의 필터를 필터링하고 있습니다.

$(this).siblings('.button2').children('.upvoteimage').load('upvote.php?box=' + box); 
2

HTML을

<div class='commentfloatleft'> 
        <button type='button' class='button2' onclick='upvote(this,\x27$id8\x27)'> 
         <img src='images/likebutton.png' class='upvoteimage' width='12' height='12' alt='PearlSquirrel'/> 
        </button> 
        <button type='button' class='button2' onclick='downvote($id8)'> 
         <img src='images/unlikebutton.png' width='12' height='12' alt='PearlSquirrel'/> 
        </button> 
       </div> 

당신의 jQuery 코드를 시도 :

은 무엇 당신이하고 싶은 것은 형제, 다음 클래스 upvoteimage의 자녀를 선택 참조입니다 :

function upvote(obj,box){ 
$(obj).children('.upvoteimage').load('upvote.php?box=' + box); 
} 

를 발생하지 않는 Y 16,

0

그 아이가 아닌 형제는 당신은 내가 당신이 이벤트를 바인딩 .on()를 사용하는 대신해야하는지 같은 일부

0

작동합니다 생각

$(this).siblings('.button2' > '.upvoteimage') 

을 시도 할 수 있습니다 이잖아 그것을 인라인으로합니다.

upvote 함수에서 this은 버튼이 클릭되지 않은 창 개체입니다.

upvote 클래스가있는 img는 형제가 아닌 버튼의 자식입니다.

당신은

관련 문제