2009-07-27 34 views
23

div内の要素の選択とフィルタリングに問題があります。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"); 
}); 

問題は、私はdivの内側に何かをクリックするたびに警告を取得しています。
しかし、私の要件は、ユーザーがボタンをクリックしたときだけ警告することです。
私はjQueryの中の要素をフィルタリングすると、これは私がしようとしたものです:button

を使用していることを知っている:

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

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

をそれは

誰もが知っている動作しませんでしたこれを行う方法?

+9

@Erwin - 今後の参考として、このような質問を投稿するときは、[コミュニティWiki]チェックボックスをチェックしないでください。これは有効なプログラミングの質問であり、ユーザーはそれから担当者を獲得する必要があります –

答えて

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

使用、特定のIDボタン - div要素内のすべてのボタンについては

<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

like-レディ機能の内側に私のすべてのjqueryの機能を置くのが好き、

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

ところで、いくつかのbuttons-のクラスを使用しますthis

0

試用:

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