2016-10-28 3 views
2

<div class="thewrapper">の下にネストされたすべての要素のhtmlをclass = "excludeme"を除いてどのように選択できますか?jquery htmlメソッドから要素を除外

<div class="thewrapper"> 
    <div class="excludeme"> some content to exclude.... </div> 

    <div class="whatever"> 
     <span> lots of content and tags </span> 
    </div> 

    <div class="excludeme"> some content to exclude again.... </div> 

    <div class="whatever"> 
     <span> more content and tags </span> 
    </div>  

私はjqueryの.NOTをいじるとされています:いない事業​​者とちょうどそれが働いて得ることができません。例えば。以下は動作しません:

var pagehtml = $("div[class^='thewrapper']:not(div[class^='excludeme']").html(); 
var pagehtml = $('div[class^="thewrapper"]').not('[class="excludeme"]').html(); 
var pagehtml = $("div.thewrapper:not(:has(div.excludeme))").html(); 
var pagehtml = $('div:not(".excludeme")').html(); 
+0

私は質問が提供されたリンクと重複していないと思います。この問題の問題は、要素が1つもない要素のselect子です。 – Mohammad

+0

@Mohammad厳密に、子どもなしで.excludeme'下記の答えを参照してください。正確に何が尋ねられますか?重複した質問は 'html'ではなく' text'を要求しますが、基本的に原則は同じです。 –

+0

それは素晴らしいです。はい、これは私の質問に対する正確な解決策です。投稿する前に気付かないうちに申し訳ありません。答えるのは大変ありがとう!! – Jason908

答えて

0
あなたは簡単除く要素の内容(テキストまたはHTML)を取得するには、このマイクロjQueryプラグイン I've created for a similar Questionを使用することができ

/無視特定のセレクタ:

$.fn.ignore = function(sel){ 
 
    return this.clone().find(sel||">*").remove().end(); 
 
}; 
 

 
var notExcludeme = $(".thewrapper").ignore(".excludeme").html(); 
 
console.log(notExcludeme);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 

 
<div class="thewrapper"> 
 
    <div class="excludeme"> some content to exclude.... </div> 
 

 
    <div class="whatever"> 
 
    <span> lots of content and tags </span> 
 
    </div> 
 

 
    <div class="excludeme"> some content to exclude again.... </div> 
 

 
    <div class="whatever"> 
 
    <span> more content and tags </span> 
 
    </div> 
 
    
 
</div>

0

ここでは、CSSセレクタを使用した別のソリューションです。あなたも同様のやり方でやってみた。 RokoC.Buljan @

$(function() { 
 
    $('.thewrapper :not(.excludeme)').css('background', 'aqua'); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 

 
<div class="thewrapper"> 
 
    <div class="excludeme"> 
 
    some content to exclude.... 
 
    </div> 
 

 
    <div class="whatever"> 
 
    <span> lots of content and tags </span> 
 
    </div> 
 

 
    <div class="excludeme"> 
 
    some content to exclude again.... 
 
    </div> 
 

 
    <div class="whatever"> 
 
    <span> more content and tags </span> 
 
    </div> 
 
</div>

+0

'.css()'のコードは動作しますが、正しいhtmlは返しません。 – Mohammad

+0

ちょうど微調整のビートが必要です。 CSSセレクタとjqueryを使用してHTMLを取得するコードを持つ[JSBinの例](http://jsbin.com/firojew/edit?html,js,console)です。 – Mihailo

関連する問題