简体   繁体   English

jQuery .replaceWith无法正常工作

[英]Jquery .replaceWith not working proper

i have a jquery code that is preventing a link to go to that link but executing it. 我有一个jquery代码,它阻止链接转到该链接但执行该链接。 The problem i have is that after it executs the script and script is returning data i want to replace it with a new one but with the same class. 我的问题是执行脚本并返回数据后,我想用一个新的但具有相同类的脚本替换它。 The replace is doing inside the dom but next time i press that link is not prevening going to that link but the class is the same, here is my code: 替换操作是在dom内部进行的,但是下次我按该链接不会阻止转到该链接,但该类是相同的,这是我的代码:

<script>
$(".recomanda").click(function(e){
    e.preventDefault();
    var test=$(this);

    var href = $(this).attr('href');


    $.getJSON(href, function(data) {

        if(data.recom==1)
        {
        $(test).replaceWith('<a class="recomanda" href="app/recomanda_produs.php?id=' + data.id + '&recom=' + data.recom + '">Recomandat</a> ');

        }
        if(data.recom==0)
        {
        $(test).replaceWith('<a class="recomanda" href="app/recomanda_produs.php?id=' + data.id + '&recom=' + data.recom + '">Recomanda</a> ');

        }

    });
});
</script>

html html

<a class="recomanda" href="app/recomanda_produs.php?id='.$row['ID_Produs'].'&recom=0">Recomanda</a>

是的,我之前也遇到了这个问题,这是因为当您在ready()上附加单击以推荐时,但是当ajax加载时,ready()中的所有内容都不会再次触发,这就是为什么您需要将事件附加到非动态元素,让它找到它的子选择器。

$('body').on('click', '.recomanda', function() {}); 

When you call a replaceWith actually you are removing elements that are bound to onclick handler: 实际上,当您调用replaceWith您将删除绑定到onclick处理程序的元素:

.replaceWith() 。用。。。来代替()

Description: Replace each element in the set of matched elements with the provided new content and return the set of elements that was removed. 说明:用提供的新内容替换匹配元素集中的每个元素,并返回被删除的元素集。

The main idea is that you handler must be bound to the same element (that is not removed when clicking). 主要思想是您的处理程序必须绑定到相同的元素(单击时不会删除)。 So instead of using replaceWith method use method that modify existing element like this: 因此, replaceWith使用replaceWith方法, replaceWith使用像这样修改现有元素的方法:

test.attr('href', blablabla);

And this is not a problem, but second time you don't need to use $ with test variable. 这不是问题,但是第二次您不需要将$test变量一起使用。

You need to delegate the event to a parent so that it can be applied to specific children wether they exist now or in the future. 您需要将事件委托给父项,以便可以将其应用于当前或将来存在的特定子项。

See: http://learn.jquery.com/events/event-delegation/ 参见: http : //learn.jquery.com/events/event-delegation/

$("body").on("click", ".recomanda", function(e){
    e.preventDefault();
    var test=$(this);
    var href = $(this).attr('href');

    $.getJSON(href, function(data) {
        if(data.recom==1){
            $(test).replaceWith('<a class="recomanda" href="app/recomanda_produs.php?id=' + data.id + '&recom=' + data.recom + '">Recomanda"+((data.recom==1)?"t":"")+"</a> ');
        }
        if(data.recom==0){
            $(test).replaceWith('<a class="recomanda" href="app/recomanda_produs.php?id=' + data.id + '&recom=' + data.recom + '">Recomanda</a> ');
        }
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM