最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Correct way of binding click event to list item in jquery - Stack Overflow

programmeradmin2浏览0评论

HTML CODE:

<ul id="sortable1" class="connectedSortable ui-sortable">
<li class="ui-state-default" id="1">Name1</li>
<li class="ui-state-default" id="2">Name2</li>
<li class="ui-state-default" id="3">Name3</li>
<li class="ui-state-default" id="4">Name4</li>
<li class="ui-state-default" id="5">Name5</li>

JQUERY CODE:

$(function() {
   $("#sortable1 li").each(function()
   {
        $(this).on("click",make($(this)));
   });

   function make($li)
   {
        alert("hello");
   }
});

fiddle :

/

when i run the fiddle , it will automatically displays the alert for five list items. actually i have binded click event for list item so whenever the list is clicked it needs to call the function.

but why the above "make" function triggered five times before am clicking the list. after clicking the list item nothing happened. what is the problem ?

when i write the function code in-line everything works as expected.

Working code:

 $("#sortable1 li").each(function()
 {
    $(this).on("click",function()
    {
      alert("hello");
    });
 });

what is the correct behavior ?

HTML CODE:

<ul id="sortable1" class="connectedSortable ui-sortable">
<li class="ui-state-default" id="1">Name1</li>
<li class="ui-state-default" id="2">Name2</li>
<li class="ui-state-default" id="3">Name3</li>
<li class="ui-state-default" id="4">Name4</li>
<li class="ui-state-default" id="5">Name5</li>

JQUERY CODE:

$(function() {
   $("#sortable1 li").each(function()
   {
        $(this).on("click",make($(this)));
   });

   function make($li)
   {
        alert("hello");
   }
});

fiddle :

http://jsfiddle/gztRq/433/

when i run the fiddle , it will automatically displays the alert for five list items. actually i have binded click event for list item so whenever the list is clicked it needs to call the function.

but why the above "make" function triggered five times before am clicking the list. after clicking the list item nothing happened. what is the problem ?

when i write the function code in-line everything works as expected.

Working code:

 $("#sortable1 li").each(function()
 {
    $(this).on("click",function()
    {
      alert("hello");
    });
 });

what is the correct behavior ?

Share Improve this question edited Feb 26, 2014 at 10:55 dreamweiver 6,0022 gold badges26 silver badges39 bronze badges asked Feb 26, 2014 at 10:46 SivaRajiniSivaRajini 7,37522 gold badges84 silver badges129 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 3

Remove the $.each() loop pletely and just use:

$('#sortable1').on('click', 'li', function() {
    alert("Hello"); // Or make($(this)); if you still want that extra function
});

JSFiddle demo.

You call your function, don't add "(", ")". Just write name function (is reference). And this, is already define for the event function. http://jsfiddle/gztRq/437/

$(function() {    
   $("#sortable1 li").each(function() {
       $(this).click(make);
   });
   function make() {
        alert("hello "+$(this).text()); 
   }
});

You should use:

 $(this).on("click",function(){make($(this))});

Demo

Try this,

$("#sortable1 li").click(function(){
     make($(this));
});

you can try:

$(document).on("click","sortable1 li",function(){

});
发布评论

评论列表(0)

  1. 暂无评论