[英]Jquery: After removing content with a click how do I reset the button with the one I triggered the add function?
So I have this function where I add content on click to a "Favorites page", but when I click the button to remove it from the Favorites tab it removes the content but the button on the main page does not reset, the question is, how do I reset the button to it's original state after clicking the "Unfavorite" button? 因此,我具有此功能,可以在单击时将内容添加到“收藏夹页面”,但是当我单击从“收藏夹”选项卡中将其删除的按钮时,它将删除内容,但是主页上的按钮没有重置,问题是,单击“不喜欢的”按钮后,如何将按钮重置为其原始状态?
https://jsfiddle.net/yjL7L6g7/3/ https://jsfiddle.net/yjL7L6g7/3/
$(document).ready(function() {
$('button').click(function() {
if ($(this).html() == 'Favorite') {
var $favorited = $(this).parent().parent().parent().clone();
$(this).html('Favorited');
$favorited.find('button').html('Unfavorite');
$($favorited).click(function() {
$(this).remove();
});
$('#favorites').append($favorited);
}
});
});
And my second question related to this code is, how do I add a button to be on the same row with the content that is being added to the "Favorites"? 与该代码有关的第二个问题是,如何添加与添加到“收藏夹”中的内容位于同一行的按钮? I tried a simple .append();
我尝试了一个简单的.append(); but it did not suffice as the button got placed in a new row, will .css() suffice?
但是,由于按钮被放置在新行中,这还不够,.css()是否足够? The questions might be stupid but I am still on my first steps in learning jquery
这些问题可能很愚蠢,但我仍在学习jquery的第一步
I'd avoid cloning if possible because there are simpler ways to do what you're trying to do. 如果可能的话,我会避免克隆,因为有更简单的方法可以完成您要尝试的操作。 The code below will create a new button and add it to your favorites page.
下面的代码将创建一个新按钮,并将其添加到您的收藏夹页面。 It will also attach an event to the Remove button to change the text of the Favorited button as well as remove itself after being clicked.
它还会将事件附加到“删除”按钮上,以更改“收藏夹”按钮的文本以及在单击后自行删除。
$(document).ready(function () {
$('button').click(function () {
if ($(this).html() == 'Favorite') {
var that = this;
$(this).html('Favorited');
var id = $(this).attr('id');
$('#favorites').append('<button id="' + id + 'Remove" class="ui-btn">Remove</button>');
$('#' + id + 'Remove').click(function () {
$(this).remove();
$(that).html('Favorite');
});
}
});
});
As for your second question, there is CSS that allows elements to live on the same line. 至于第二个问题,有CSS允许元素位于同一行。 For example, if you have two buttons that you want on the same line, it would look something like this:
例如,如果在同一行上有两个按钮,则看起来像这样:
<button style="display: inline;">Button1</button>
<button style="display: inline;">Button2</button>
Let me know if you have any questions. 如果您有任何疑问,请告诉我。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.