4

変数にhtmlを保存しました

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

たくさんの html と削除したいボタンが 1 つあります。ID「myButton」を持っています。変数に保存されているhtmlから削除するにはどうすればよいですか

4

5 に答える 5

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 */
于 2013-02-07T17:07:25.480 に答える
1

これを試して:

itinerary.filter(function() { return $(this).not("#myButton"); });
于 2013-02-07T16:59:37.963 に答える
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 メソッドを使用して上記で行ったことをよりうまく行うことができます。

于 2013-02-07T16:58:10.370 に答える
0

id で要素を見つけて削除するだけです。

$('#myButton').remove()

次に、旅程を取得します。

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

// 上記は DOM からボタンを削除します。ボタンを DOM から取り出さずに html を取得するには、次のようにします。

var itinerary = $('.events_today').clone(true).find('#myButton').remove().end().html();
于 2013-02-07T16:57:45.590 に答える