我有一样的id多个按钮: #recent_post_button_54
,#recent_post_button_55
等如何在多个按钮设置点击事件
当我点击按钮#recent_post_button_54
我希望它切换corresponsing #recent_post_content_54
格。
我的代码是:
jQuery(document).ready(function($) {
var buttons = $('.recent_post_header button');
for (var i = 0; i< buttons.length; i++) {
var id = (buttons[i].id).match(/\d+$/)[0];
$("#recent_post_button_"+id).click(function() {
console.log("I've clicked #recent_post_button_"+id);
$("#recent_post_content_"+id).toggle();
});
}
});
我有IDS 54,52,50三个按钮。但是通过这段代码,我得到了所有按钮的I've clicked #recent_post_button_50
,并且所有按钮仅切换与id 50
的最后一个内容div。
这段代码有什么问题?
你能提供一个你的HTML样本,你的按钮的父元素 – Anonymous0day