2010-12-20 66 views
1

我有以下一段代码:改变事件的执行顺序

<html> 
<head> 
<script src="myplugin.js" /> 
<script> 
    $(document).ready(function() { 
    $('#mytable tbody tr').live('click', function() { 
    alert('Hi'); 
    }); 
    }); 
</script> 
</head> 
<body> 
<script> 
    $('#mytable').myplugin(); 
</script> 

<table id="mytable"> 
    <tbody> 
    <tr> 
    <td>Something</td> 
    </tr> 
    </tbody> 
</table> 
</body> 
</html> 

myplugin.js code 

..... 
return this.each(function(index, id) { 
$(id + ' tbody tr').live('click', function() { 
    alert('Hello'); 
}); 
}); 
..... 

根据该circustances,executiong的顺序将是:

alert('Hi'); 
alert('Hello'); 

,但我想是相反的。 可以更改执行顺序吗? 谢谢

回答

0

这是不是最漂亮的方式,但你可以推迟你的第一个电话是这样的:

$(document).ready(function() { 
    $('#mytable tbody tr').live('click', function() { 
    window.setTimeout(function() { 
     alert('Hi'); 
    }, 0); 
    }); 
    }); 

但也许有一个“官方”的方式,以及...

+0

不错,这工作:D – Diego 2010-12-20 16:00:56

0

你应该交换脚本的顺序。

+1

这是行不通的。不管怎样,谢谢你 – Diego 2010-12-20 16:01:26