从包含HTML的变量中删除HTML元素

时间:2022-08-27 12:33:05

Hi guys I've got a jQuery var that contains HTML that I'm appending to a page.

大家好,我有一个包含HTML的jQuery var,我将其附加到页面。

I need to remove a HTML element before I append it to the page but I can't seem to filter it.

我需要在将其附加到页面之前删除HTML元素,但我似乎无法过滤它。

for example the variable contains;

例如变量包含;

<div class="block1"></div>
<div class="block2"></div>

I have tried filtering like this before I append;

在我追加之前,我尝试过这样的过滤;

var mydata = $(mydata).filter('.block1');

and

var mydata = mydata.filter('.block1');

but none of these work. Any suggestions?

但这些都不起作用。有什么建议么?

5 个解决方案

#1


1  

Filter applied directly to string sometimes give string result, its safe to add element to DOM and then apply filters. Assign html to some temporary element in the DOM, Get the filtered items from this temporary element and assign it to desired element.

直接应用于字符串的过滤器有时会给出字符串结果,可以安全地将元素添加到DOM然后应用过滤器。将html分配给DOM中的某个临时元素,从此临时元素中获取已过滤的项目并将其分配给所需的元素。

$('#TemporaryElemntToAddHtml').html(mydata);

    $('#ElemntToAddHtml .block1').each(function(){
         $('#ElemntToAddHtml').append($(this));
    });

$('#TemporaryElemntToAddHtml').remove();

#2


1  

Try .remove()

试试.remove()

$('.block1').remove();

OR

要么

var mydata = $(mydata).remove('.block1');

#3


0  

$.filter doesn't do what you think it does.

$ .filter不会按照您的想法执行操作。

http://api.jquery.com/filter/

http://api.jquery.com/filter/

I would try $(mydata).remove('.block1');

我会尝试$(mydata).remove('。block1');

#4


0  

Did you try the .remove function?

你试过.remove功能吗?

  var mydata = $('<div class="block1"></div>
                 <div class="block2"></div>');
  mydata.remove('.block1');

#5


0  

Use the splice native method to remove at the element index, and remove 1 item.

使用splice native方法删除元素索引,并删除1项。

mydata.splice( mydata.filter('.block1').index(), 1 );

Note with that splice do not add mydata = mydata.splice(..) as that will return the removed element.

请注意,拼接不添加mydata = mydata.splice(..),因为它将返回已删除的元素。

Otherwise use jQuery's remove method as some people already said.

有些人已经说过,否则使用jQuery的删除方法。

#1


1  

Filter applied directly to string sometimes give string result, its safe to add element to DOM and then apply filters. Assign html to some temporary element in the DOM, Get the filtered items from this temporary element and assign it to desired element.

直接应用于字符串的过滤器有时会给出字符串结果,可以安全地将元素添加到DOM然后应用过滤器。将html分配给DOM中的某个临时元素,从此临时元素中获取已过滤的项目并将其分配给所需的元素。

$('#TemporaryElemntToAddHtml').html(mydata);

    $('#ElemntToAddHtml .block1').each(function(){
         $('#ElemntToAddHtml').append($(this));
    });

$('#TemporaryElemntToAddHtml').remove();

#2


1  

Try .remove()

试试.remove()

$('.block1').remove();

OR

要么

var mydata = $(mydata).remove('.block1');

#3


0  

$.filter doesn't do what you think it does.

$ .filter不会按照您的想法执行操作。

http://api.jquery.com/filter/

http://api.jquery.com/filter/

I would try $(mydata).remove('.block1');

我会尝试$(mydata).remove('。block1');

#4


0  

Did you try the .remove function?

你试过.remove功能吗?

  var mydata = $('<div class="block1"></div>
                 <div class="block2"></div>');
  mydata.remove('.block1');

#5


0  

Use the splice native method to remove at the element index, and remove 1 item.

使用splice native方法删除元素索引,并删除1项。

mydata.splice( mydata.filter('.block1').index(), 1 );

Note with that splice do not add mydata = mydata.splice(..) as that will return the removed element.

请注意,拼接不添加mydata = mydata.splice(..),因为它将返回已删除的元素。

Otherwise use jQuery's remove method as some people already said.

有些人已经说过,否则使用jQuery的删除方法。