我很难让一些 jQuery 函数正常工作。
我的网站上有一个点赞按钮,该按钮在添加新项目之前一直有效,然后停止工作。当我有以下代码时,这有效:
$('.like').toggle(
function() {
console.log('href');
}, function() {
console.log('rel');
}
);
我的一个朋友指导我使用事件冒泡,但我很难让它发挥作用。
这是我目前所拥有的。
简单的 HTML
<button href='123.html' rel='456.html' class='like'>Click here</button>
jQuery,包裹在 $(document).ready( ...我尝试了切换功能,似乎按钮在第一次单击时处于休眠状态,然后它突然醒来并执行事件。
$('body').click(function(event) {
if ($(event.target).is('.like')) {
var $like = $(event.target);
$like.toggle(
function() {
console.log('href');
}, function() {
console.log('rel');
}
);
}
});
为了在添加新项目时继续工作并确保按钮不会以这种方式运行,代码应该是什么样的
这是一个小提琴。