我有一个问题。我有8个按钮,它们具有唯一的递增ID和共享类。
<button class="btnChapter" id="btnChapter_1" value="1">1</button>
<button class="btnChapter" id="btnChapter_2" value="2">2</button>
<button class="btnChapter" id="btnChapter_3" value="3">3</button>
...
为了防止我复制代码,我将click事件绑定到btnChapter类,而不是将事件单独绑定到每个按钮的ID。
$('.btnChapter').click(function(){ .. do stuff .. });
如何仅针对#btnChapter_2触发()click()事件?以下似乎不起作用。
$('#btnChapter_2').click()
答案 0 :(得分:1)
你的代码非常好。
在我看来,你唯一可能做错的事情就是在ready
事件之后不执行此代码。
我还建议使用live
。
试试这个:
$(function() {
$('.btnChapter').live('click', function(){
//blah
});
//...
$('#btnChapter_2').click();
});
答案 1 :(得分:0)