add click event to all li which has id - using jquery
I need to add the click event to all the li's
inside a div which has id to the li.
The following script changes for all the li's:
<ul>
<li>
<h4>My Header</h4></li>
<li id="myli1">My li 1</li>
<li id="myli2">My li 2</li></ul>
$("#mydiv li").live('click', function(e) {
alert($(开发者_如何学JAVAthis).attr("id"));
});
I need to add the click events for only the li which has the id attribute.
This should do it:
$("#mydiv li[id]").live('click', function(e) {
alert($(this).attr("id"));
});
You can take the solution of @ Greg or you can select the types of Id's. see jQuery Selector
$("#mydiv li[id^='myli']").live('click', function(e) {
alert($(this).attr("id"));
});
精彩评论