たとえば、phpにはクライアントのリストがあります。
foreach($clients as $client)
echo '<li id="client_'.$client->id.'">' . $client->name . '</li>';
クリックイベントを使用せずに各リストアイテムにバインドする正しい方法は何ですか
onclick="my_function(the_elements_id)"
リストアイテムの右側。
id="client_list" の ul があるとします。
$("#client_list li").click(function()
{
var myid = $(this).attr("id"); // the clicked on items id
// do your thing here.
}):
そして、現在および将来のすべての一致する要素をバインドする非常にクールな「ライブ」方法。これは、Ajax を使用している場合に便利で、イベントを再バインドして、ajax 経由で取得した画像などを取得する必要があります。
詳細情報 @ http://docs.jquery.com/Events/live#typefn
$("p").live("click", function(){
alert( $(this).text() );
});
$(document).ready(function(){
$("li).click(function(){
var id = $(this).attr("id");
your_function(id); //The function you want to run with the object's ID as a parameter.
});
});