我将html保存在变量中
var itinerary = $('.events_today').html() ;
我有很多html和一个我要删除的按钮。它的id为“myButton”。如何从保存在我的变量
中的html中删除它答案 0 :(得分:10)
我建议采用这种方法
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 */
答案 1 :(得分:1)
试试这个:
itinerary.filter(function() { return $(this).not("#myButton"); });
答案 2 :(得分:0)
只需找到元素的ID并删除:
$('#myButton').remove()
然后得到你的行程:
var itinerary = $('.events_today').html() ;
//以上将删除DOM中的按钮。要在没有从DOM中取出按钮的情况下获取html,您可以执行以下操作:
var itinerary = $('.events_today').clone(true).find('#myButton').remove().end().html();
答案 3 :(得分:0)
找到集合中的元素并将其移除。
$('.events_today').find('#myButton').remove();
编辑:在意识到我可能误解了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();
更新:根据@Fabrizio Calderan的回答,你可以用jQuery方法更好地完成我上面做的事情。
答案 4 :(得分:0)
试试这个:
var html = $('#myHtml').clone(true).find('#elementThatYouWantToRemove').remove().end().html();