简体   繁体   English

jQuery:单击删除内容后,如何使用触发添加功能的按钮重置按钮?

[英]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.

相关问题 如何防止重复触发自定义事件–或可以“重置” jQuery .one()函数? - How can I prevent a custom event from being triggered repeatedly – or can I “reset” the jQuery .one() function? jQuery如何在单击按钮后在具有其他功能之前延迟并删除和添加类 - Jquery how do i remove and add a class after button click with time delay before other functions 替换内容后如何触发jQuery .click()? - How do I trigger jQuery .click() after replacing content? 如何在计数器中添加重置按钮? - How do I add a reset button in a counter? 当我单击 Javascript 中另一个按钮的内容时,如何关闭一个按钮内容 - How do i make one button content close when i click another button's content in Javascript 为什么在触发其中的查询之前需要两次单击“重置”按钮? - Why do I need to click reset button twice before the jqueries in it is triggered? 如何在CasperJS中保存通过单击按钮触发的文件? - How do I save a file triggered by button click in CasperJS? 在单击警报后删除所述按钮之前,如何强制将按钮文本更改为粗体? - How do I force to change the button text to bold before removing the said button after an alert on click? 单击后如何获取内容幻灯片以重置其自动滚动计时器? - How do I get my content slideshow to reset its auto-scroll timer after a click? 单击按钮后如何禁用拖动 function - How do I disable drag function after click on a button
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM