1

HTML:

<p><br style="clear:both;"></p>

プロジェクトにウィキペディア API を使用しており、ウィキペディアの生の HTML 出力をクリーンアップしており、この問題に苦労しています。<p>jQueryを使用してDOMからこの特定の要素を削除するにはどうすればよい.remove();ですか? 内部にある<p>要素のみを削除したい。<br style="clear:both;">

前もって感謝します!私はこの問題に苦労してきました。

4

6 に答える 6

5

So, you're trying to remove all elements that directly contain a <br> with the given style attribute?

$('br[style="clear:both;"]').parent().remove();

This is decently conservative in that it only matches br tags of the form you describe—though it might be worth being even more conservative by sticking a p selector in that parent call. But, it's also sufficiently conservative that it might not catch small variations on this markup, so keep an eye out.

于 2013-07-17T22:16:33.243 に答える
3

子がそれ自体であると仮定するとbr、コードは次のようになります -

$('p>br').closest('p').remove();

または使用

$('p>br[style="clear:both;"]').closest('p').remove();

または親関数を使用します

$('br[style="clear:both;"]').parent('p').remove(); 

しかし、それをしないでください

$('br[style="clear:both;"]').parent().remove(); // Don't use this

親がそうでないには多くのbr可能性があるためdomp

于 2013-07-17T22:15:05.677 に答える
0
$('p').filter(function(i) {
    return $(this).children('br').css('clear') == 'both';
}).remove();
于 2013-07-17T22:18:26.413 に答える
0
$('p').each(function(){
    if($(this).children('br').length == 1) {
        $(this).remove();
    }
});

これは私にとってはうまくいきました。「クリア:両方;」のようです。ブラウザによって生成されました。私がこれを理解できるように、すべてのアイデアと提案をありがとう!

于 2013-07-17T23:14:55.583 に答える