2013-08-28 2 views
-1

이 내 페이지의 3 개 섹션에서이 HTML 코드가 동작하지 않습니다 :이의 jQuery .each()

<div class="pjesmarrje"> 
    <a href="#" onclick="window.open('https://www.facebook.com/sharer/sharer.php?u='+encodeURIComponent(location.href),'facebook-share-dialog','width=626,height=436'); return false;"> 
     <div></div> 
     <span>Kliko këtu për pjesëmarrje</span> 
    </a> 
</div> 

그리고를, 나는 때의 클릭 된 내부 사업부의 배경 이미지를 변경하려고 . 이 jQuery 코드가 있습니다 :

$(document).ready(function() { 
    $(".pjesmarrje").click(function() { 
     $(".pjesmarrje div").css("background-image", "url(images/mumanin_s2.png)"); 
    }); 
}); 

요소 중 하나를 클릭하면 다른 모든 요소도 배경 이미지가 변경됩니다. 나는 그 특정 요소가 클릭되었을 때만 bg 이미지가 변경되기를 원합니다. .each() 함수를 사용하려고했지만 작동하지 않았습니다.

도움을 주시면 감사하겠습니다. 감사.

+0

$ (this) .find ('div') .css ("background-image", "url (images/mumanin_s2.png)"); 클릭 한 내부의 div를 목표로하고 싶습니다. $ (this)가 클릭 된 요소를 나타냅니다. – Huangism

+0

이 질문은 jQuery .each()와 어떤 관련이 있습니까? – Divey

+0

자신이 알고 있다면 StackOverflow를 사용하지 않을 것입니다. 저는 jQuery 초보자입니다. –

답변

1

당신은 범위이라고 무엇을 잃고있어.

$(document).ready(function() { 
    $(".pjesmarrje").click(function() { 
     // `this` is a reference to the `.pjesmarrje` that triggered the click 
     // event. and, within that `<div>` we want to find the `<div>` whose 
     // background we want to change. 
     $("div", this).css("background-image", "url(images/mumanin_s2.png)"); 
    }); 
}); 

주 두 번째 인수 : $(selector, scope) 당신이 원하는 경우에 당신이 뭔가를 원하는 .pjesmarrje 특정 _that 내에서 작동합니다. 즉, <div>에 대해서만 을 클릭하면 .pjesmarrje (페이지의 일부가 아님)을 클릭하면됩니다.

그리고 음식 생각에 대한 : $('div', this)$(this).find('div')과 동의어이다.

1
$(document).ready(function() { 
    $(".pjesmarrje").click(function() { 
     $(this).find("div").css("background-image", "url(images/mumanin_s2.png)"); 
    }); 
});