2009-07-27 4 views
23

div 요소를 선택하고 필터링하는 데 문제가 있습니다.jQuery div 내 요소 선택 및 필터링

HTML :

<div id="wrapper"> 
    <input type="text" value="you can edit me"> 
    <input type="button" value="click me"> 
</div> 

jQuery를 :

$("#wrapper").children().click(function() { 
    alert("hi there"); 
}); 

문제는 내가 사업부 제품 내부를 클릭 할 때마다 경고를받을된다.
하지만 내 요구 사항은 사용자가 버튼을 클릭 할 때만 경고합니다.

$("#wrapper").children(":button").click(function() { 
    alert("hi there"); 
}); 

$("#wrapper").children().filter(":button").click(function() { 
    alert("hi there"); 
}); 

을 그것은

누구나 알고 작동하지 않았다 :
나는 :button

이 내가 시도한 것입니다 jQuery를의 요소를 필터링하여 사용하고 있음을 알고있다 어떻게해야합니까?

+9

@Erwin - 향후 참고할 수 있도록이 질문을 게시 할 때 "커뮤니티 위키"확인란을 선택하지 마십시오. 이것은 유효한 프로그래밍 질문이며 사용자는이 프로그램에서 담당자를 얻어야합니다. –

답변

42
$("#wrapper input[type=button]").click(function() { 
    alert("hi there"); 
}); 
1

사용 특정에 대한 ID 버튼 - 사업부의 모든 버튼의

<div id="wrapper"> 
    <input type="text" value="you can edit me"> 
    <input type="button" id='btnMyButton' value="click me"> 
    <input type="button" class='btnClass' id='btnMyButton2' value="click me 2"> 
<input type="button" class='btnClass' id='btnMyButton3' value="click me 3"> 
</div> 

$('#btnMyButton').click(function(){ 
alert("hi there"); 
}); 

, 존의 답변을 따르십시오. 내가 방법에 대한

$("#wrapper :button").click(function() { alert("I love Imogen Poots"); });

참조

$(document).ready(function(){   

}); 
0

로 삼았 준비가 함수 내에서 내 모든 JQuery와 기능을 넣어 좋아

$('.btnClass').click(function(){ 
    alert("all class"); 
}); 

BTW 일부 buttons-에 대한 클래스를 사용 this

0

시험해보기 :

$("#wrapper > input[type=button]").click(function() { 
    alert("hi there"); 
});