jquery从变量中移除html元素

时间:2022-12-08 19:46:41

I have html saved in a variable

我将html保存在一个变量中

var  itinerary =  $('.events_today').html() ;

I have lots of html and one button I want to remove. It has the id "myButton". How can I remove it from the html saved in my variable

我有很多html和一个按钮,我想删除。它有id“myButton”。如何从保存在变量中的html中删除它

5 个解决方案

#1


9  

I suggest this approach

我建议这个方法

var itinerary = $('.events_today')
                .clone(true)       /* get a clone of the element and from it */
                .find('#myButton') /* retrieve the button, then */
                .remove()          /* remove it, */
                .end()             /* return to the previous selection and */
                .html() ;          /* get the html */

#2


1  

Try this:

试试这个:

itinerary.filter(function() { return $(this).not("#myButton"); });

#3


0  

Just locate the element by it's id and remove:

只需根据元素的id定位并移除:

$('#myButton').remove()

Then get your itinerary:

然后让你的行程:

var itinerary =  $('.events_today').html() ;

// The above will remove the button from the DOM. To just get the html without having the button taken out of the DOM you can do:

//以上将从DOM中删除按钮。要获得html而不将按钮从DOM中取出,您可以这样做:

var itinerary = $('.events_today').clone(true).find('#myButton').remove().end().html();

#4


0  

Find the element in the collection and remvoe it.

在集合中找到元素并重新映射它。

$('.events_today').find('#myButton').remove();

$(' .events_today ');(#色).remove();

Edit: After realizing I might've misunderstood the OP, this might work (although not exactly very beautiful).

编辑:在意识到我可能误解了OP之后,这可能会有用(虽然不是很漂亮)。

var html = $('.events_today').html();
// Parse the html, but don't add it to the DOM
var jqHtml = $(html);
// Find and remove the element with the specified id.
jqHtml.find('#myButton').remove();
// Get the new html without the myButton element.
var result = jqHtml.html();

Update: Based on @Fabrizio Calderan's answer you can do what I did above much more nicely with jQuery methods.

更新:根据@Fabrizio Calderan的回答,您可以使用jQuery方法进行上述操作。

#5


0  

Try this:

试试这个:

var html = $('#myHtml').clone(true).find('#elementThatYouWantToRemove').remove().end().html();

#1


9  

I suggest this approach

我建议这个方法

var itinerary = $('.events_today')
                .clone(true)       /* get a clone of the element and from it */
                .find('#myButton') /* retrieve the button, then */
                .remove()          /* remove it, */
                .end()             /* return to the previous selection and */
                .html() ;          /* get the html */

#2


1  

Try this:

试试这个:

itinerary.filter(function() { return $(this).not("#myButton"); });

#3


0  

Just locate the element by it's id and remove:

只需根据元素的id定位并移除:

$('#myButton').remove()

Then get your itinerary:

然后让你的行程:

var itinerary =  $('.events_today').html() ;

// The above will remove the button from the DOM. To just get the html without having the button taken out of the DOM you can do:

//以上将从DOM中删除按钮。要获得html而不将按钮从DOM中取出,您可以这样做:

var itinerary = $('.events_today').clone(true).find('#myButton').remove().end().html();

#4


0  

Find the element in the collection and remvoe it.

在集合中找到元素并重新映射它。

$('.events_today').find('#myButton').remove();

$(' .events_today ');(#色).remove();

Edit: After realizing I might've misunderstood the OP, this might work (although not exactly very beautiful).

编辑:在意识到我可能误解了OP之后,这可能会有用(虽然不是很漂亮)。

var html = $('.events_today').html();
// Parse the html, but don't add it to the DOM
var jqHtml = $(html);
// Find and remove the element with the specified id.
jqHtml.find('#myButton').remove();
// Get the new html without the myButton element.
var result = jqHtml.html();

Update: Based on @Fabrizio Calderan's answer you can do what I did above much more nicely with jQuery methods.

更新:根据@Fabrizio Calderan的回答,您可以使用jQuery方法进行上述操作。

#5


0  

Try this:

试试这个:

var html = $('#myHtml').clone(true).find('#elementThatYouWantToRemove').remove().end().html();