変数にhtmlを保存しました
var itinerary = $('.events_today').html() ;
たくさんの html と削除したいボタンが 1 つあります。ID「myButton」を持っています。変数に保存されているhtmlから削除するにはどうすればよいですか
このアプローチを提案します
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 */
これを試して:
itinerary.filter(function() { return $(this).not("#myButton"); });
コレクション内の要素を見つけて削除します。
$('.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 メソッドを使用して上記で行ったことをよりうまく行うことができます。
id で要素を見つけて削除するだけです。
$('#myButton').remove()
次に、旅程を取得します。
var itinerary = $('.events_today').html() ;
// 上記は DOM からボタンを削除します。ボタンを DOM から取り出さずに html を取得するには、次のようにします。
var itinerary = $('.events_today').clone(true).find('#myButton').remove().end().html();