JavaScript订阅按钮仅在我单击图标而不是按钮的其余部分时起作用
我有一个订阅用户配置文件按钮,该按钮仅在单击RSS字体真棒图标而不是其余按钮时起作用。我尝试了几次重写,其中将按钮元素在单击事件中制作,但它们根本不起作用。
这是仅在单击图标元素时工作的代码:
<button class='subscribe-button profile-subscribe'>
<?php if ($profile->userSubscribed): ?>
<i class="fa-solid fa-user-check subscribe" data-user='<?= esc($profile->username);?>'></i>Subscribed
<?php else: ?>
<i class="fa-solid fa-rss subscribe" data-user='<?= esc($profile->username);?>'>
</i> Subscribe
<?php endif ?>
</button>
<script>
$(document).ready(function() {
var csrfName = "<?= csrf_token(); ?>";
var csrfHash = "<?= csrf_hash(); ?>";
// If user clicks the subscribe button
$(".subscribe").on("click", function () {
var userProfile = $(this).data("user");
$clicked_btn = $(this);
if ($clicked_btn.hasClass("fa-solid fa-rss")) {
action = "subscribe";
} else if ($clicked_btn.hasClass("fa-solid fa-user-check")) {
action = "unsubscribe";
}
$.ajax ({
url: "<?= base_url(); ?>/users/members/view_profile/<?= $profile->username;?>",
type: "post",
dataType: "json",
data: {
[csrfName]: csrfHash,
"action": action,
"user_profile": userProfile,
},
headers: {
'X-Requested-With': 'XMLHttpRequest',
},
success: function(data) {
var res = data;
csrfName = res.csrfName;
csrfHash = res.csrfHash;
if (action == "subscribe") {
$clicked_btn.removeClass("fa-solid fa-rss");
$clicked_btn.addClass("fa-solid fa-user-check");
} else if (action == "unsubscribe") {
$clicked_btn.removeClass("fa-solid fa-user-check");
$clicked_btn.addClass("fa-solid fa-rss");
}
}
});
});
});
</script>
这是使整个按钮可单击的尝试:
<button class='subscribe-button profile-subscribe subscribe' id='<?= esc($profile->username); ?>'>
<i class="fa-solid fa-rss"></i> Subscribe
</button>
<script>
$(document).ready(function() {
var csrfName = "<?= csrf_token(); ?>";
var csrfHash = "<?= csrf_hash(); ?>";
// If user clicks the subscribe button
$(".subscribe").on("click", function (event) {
var userProfile = this.id;
$clicked_btn = $(this);
if ($clicked_btn.hasClass("fa-solid fa-rss")) {
action = "subscribe";
} else if ($clicked_btn.hasClass("fa-solid fa-user-check")) {
action = "unsubscribe";
}
$.ajax ({
url: "<?= base_url(); ?>/users/members/view_profile/<?= $profile->username;?>",
type: "post",
dataType: "json",
data: {
[csrfName]: csrfHash,
"action": action,
"user_profile": userProfile,
},
headers: {
'X-Requested-With': 'XMLHttpRequest',
},
success: function(data) {
var res = data;
csrfName = res.csrfName;
csrfHash = res.csrfHash;
if ($('.subscribe').hasClass('subscribed')) {
$('.subscribe').removeClass('subscribed');
$('.subscribe').html('<i class="fa-solid fa-rss"</i>Subscribe');
} else {
$('.like').addClass('subscribed');
$('.like').html('<i class="fa-solid fa-user-check">Subscribed');
}
}
});
});
});
</script>
I have a subscribe to user profile button that only works when I click the rss font awesome icon, not the rest of the button. I have tried a couple of rewrites where I make the button element the clicked event but they don't work at all.
Here is the code that works only when clicking the icon element:
<button class='subscribe-button profile-subscribe'>
<?php if ($profile->userSubscribed): ?>
<i class="fa-solid fa-user-check subscribe" data-user='<?= esc($profile->username);?>'></i>Subscribed
<?php else: ?>
<i class="fa-solid fa-rss subscribe" data-user='<?= esc($profile->username);?>'>
</i> Subscribe
<?php endif ?>
</button>
<script>
$(document).ready(function() {
var csrfName = "<?= csrf_token(); ?>";
var csrfHash = "<?= csrf_hash(); ?>";
// If user clicks the subscribe button
$(".subscribe").on("click", function () {
var userProfile = $(this).data("user");
$clicked_btn = $(this);
if ($clicked_btn.hasClass("fa-solid fa-rss")) {
action = "subscribe";
} else if ($clicked_btn.hasClass("fa-solid fa-user-check")) {
action = "unsubscribe";
}
$.ajax ({
url: "<?= base_url(); ?>/users/members/view_profile/<?= $profile->username;?>",
type: "post",
dataType: "json",
data: {
[csrfName]: csrfHash,
"action": action,
"user_profile": userProfile,
},
headers: {
'X-Requested-With': 'XMLHttpRequest',
},
success: function(data) {
var res = data;
csrfName = res.csrfName;
csrfHash = res.csrfHash;
if (action == "subscribe") {
$clicked_btn.removeClass("fa-solid fa-rss");
$clicked_btn.addClass("fa-solid fa-user-check");
} else if (action == "unsubscribe") {
$clicked_btn.removeClass("fa-solid fa-user-check");
$clicked_btn.addClass("fa-solid fa-rss");
}
}
});
});
});
</script>
And here is one attempt at making the whole button clickable:
<button class='subscribe-button profile-subscribe subscribe' id='<?= esc($profile->username); ?>'>
<i class="fa-solid fa-rss"></i> Subscribe
</button>
<script>
$(document).ready(function() {
var csrfName = "<?= csrf_token(); ?>";
var csrfHash = "<?= csrf_hash(); ?>";
// If user clicks the subscribe button
$(".subscribe").on("click", function (event) {
var userProfile = this.id;
$clicked_btn = $(this);
if ($clicked_btn.hasClass("fa-solid fa-rss")) {
action = "subscribe";
} else if ($clicked_btn.hasClass("fa-solid fa-user-check")) {
action = "unsubscribe";
}
$.ajax ({
url: "<?= base_url(); ?>/users/members/view_profile/<?= $profile->username;?>",
type: "post",
dataType: "json",
data: {
[csrfName]: csrfHash,
"action": action,
"user_profile": userProfile,
},
headers: {
'X-Requested-With': 'XMLHttpRequest',
},
success: function(data) {
var res = data;
csrfName = res.csrfName;
csrfHash = res.csrfHash;
if ($('.subscribe').hasClass('subscribed')) {
$('.subscribe').removeClass('subscribed');
$('.subscribe').html('<i class="fa-solid fa-rss"</i>Subscribe');
} else {
$('.like').addClass('subscribed');
$('.like').html('<i class="fa-solid fa-user-check">Subscribed');
}
}
});
});
});
</script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您有正确的想法,可以更改单击处理程序以选择按钮而不是图标,但是您错过了此后的步骤,这改变了您检测到是“订阅”还是“取消订阅”的方式。当单击处理程序在图标上时,该类直接在单击元素上,但是当您更改为父按钮时,您必须选择“到按钮的孩子”以查看图标上的类,以确定是否订阅“或“退订”。这应该很容易解决:
You have the right idea with changing your click handler to select your button instead of the icon, but you missed the step after that, which is changing how you detect whether it's a "subscribe" or "unsubscribe" that was clicked. When the click handler was on the icon, the class was on the clicked element directly, but when you changed to the parent button, you have to select down into the button's children to look at the class on the icon to determine if it's "subscribe" or "unsubscribe". It should be an easy fix: