简体   繁体   English

如何删除“附加元素”?

[英]How to remove an "append element"?

I am appending a span with onclick.我正在用 onclick 附加一个跨度。 I cant change this because I am getting an HTML via PHP from another website.我无法更改这一点,因为我正在通过 PHP 从另一个网站获取 HTML。

So, I am adding in a table some informations like this:所以,我在表格中添加了一些这样的信息:

            function getdados(id) {
              $('#loader').fadeIn();
              $.ajax({
                type: "GET",
                url: 'index.php',
                data: { action: 'getdados', id: id },
                success: function(response) {
                    if(count > 1) {
                        $('.remove').remove();
                        console.log(count);
                    }
                    response = response.replace(/\+/g, '%20');
                    var str = response.split("%");
                    var cval = str[0];
                    for (var i = 1; i < str.length; i++) {
                        cval += String.fromCharCode(parseInt(str[i].substring(0,2),16))+str[i].substring(2);
                    }
                    jQuery.event.trigger("ajaxStop");

                    $('.resultados').append(cval);

                    var titles = $(".title").map(function() { return $(this).text(); }).get(),
                        dados  = $(".dados").map(function() { return $(this).text(); }).get(),
                        header = $(".headertitle").map(function() { return $(this).text(); }).get();

                    for(var i = 0; i < dados.length; i++) {
                        switch(titles[i]) {
                            case 'Naturesa: ':
                                titles[i] = 'Natureza: ';
                                dados[i] = dados[i] + ' - ' + dados[i+1];
                                dados.splice(i+1, 1);
                                titles.splice(i+1, 1);
                                break;
                            case 'DOC: : ':
                                titles[i] = 'DOC: ';
                                break;
                            case 'Atividade primária: ':
                                dados[i] = dados[i] + ' - ' + dados[i+1];
                                dados.splice(i+1, 1);
                                titles.splice(i+1, 1);
                                break;
                            case 'Atividade secundária: ':
                                dados[i] = dados[i] + ' - ' + dados[i+1];
                                dados.splice(i+1, 1);
                                titles.splice(i+1, 1);
                                break;
                        }
                        $('#results-2 table').append('<tr class="remove"><th>'+ titles[i] +'</th><td>'+ dados[i] +'</td></tr>');
                    }

                    for(var i = 0; i < header.length; i++) {
                        var link = $('.headertitle').eq(i).nextUntil('.headertitle').map(function() {
                            if(this.localName == 'span')
                                return '<tr class="remove"><td colspan="2">'+this.outerHTML+'</td></tr>';
                        }).get().join('');
                        $('#results-2 table').append('<tr class="remove rowTable"><th colspan="2">'+ header[i] +'</th></tr>'+link);
                    }

                    $('#loader').fadeOut();
                },
                error : function(transport) {
                    jQuery.event.trigger("ajaxStop");
                }
            });
        }

I need to remove all tr class="remove" from the table, but I think it is not possible :(我需要从表中删除所有 tr class="remove",但我认为这是不可能的:(

Someone can help me?有人可以帮助我吗?

Replace the following code:替换以下代码:

$('#results-2 table').append('<tr class="remove rowTable"><th colspan="2">'+ header[i] +'</th></tr>'+link);

With this way, which has a handle to it:通过这种方式,它有一个句柄:

var $tr = $('<tr class="remove rowTable"><th colspan="2">'+ header[i] +'</th></tr>' + link);
$tr.appendTo('#results-2 table');

Keeping the $tr , you can reuse using:保留$tr ,您可以使用以下方法重用:

$tr.remove();

Or, the old school way, you can find the .remove :或者,老派的方式,你可以找到.remove

$("tr.remove").remove();

If I don't understand your question, may be you just need to remove the class remove from the elements, use:如果我不明白您的问题,可能您只需要从元素中删除类remove ,请使用:

$("tr.remove").removeClass("remove");

使用此行删除带有 class remove 的 tr。

jQuery("tr.remove").remove()

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

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