1

divタグをpタグに置き換えたいのですが、divタグにクラスがない場合に限ります。

したがって、この:

<div class="myDiv">
   <div>sdfglkhj sdfgkhl sdfkhgl sdf</div>
   <div>dsf osdfghjksdfg hsdfg</div>
</div>

になります:

<div class="myDiv">
   <p>sdfglkhj sdfgkhl sdfkhgl sdf</p>
   <p>dsf osdfghjksdfg hsdfg</p>
</div>

試しまし.replace("<div>", "<p>").replace("</div>","</p>")たが、これで終了タグがクラスに置き換えられます。

4

4 に答える 4

3

これは、:not()セレクターと.unwrap() .wrap()

$('div:not([class])').contents().unwrap().wrap('<p/>');

http://jsfiddle.net/matthewbj/ujLHk/

于 2012-10-04T15:43:08.253 に答える
1
$(document).ready(function () {

  $('div').each(function () {
    var html = this.innerHTML;
    if (! $(this).attr('class')) {
       $(this).replaceWith('<p>' + html + '</p>');
    }
  });
});
于 2012-10-04T15:36:27.043 に答える
0

これは、jQueryセレクターを使用してクラスなしでdivを取得するのに役立つ場合があります。

jQueryは、クラス属性を持たないすべてのdivを取得します

于 2012-10-04T15:32:56.940 に答える
0
$(document).ready(function()
{
    $('.myDiv div').contents().unwrap().wrap('<p/>');
});

また

$(document).ready(function()
{
    $('.myDiv div').each(function()
    {
        $(this).replaceWith($('<p>' + $(this).html() + '</p>'))
    });
});
于 2012-10-04T15:35:19.787 に答える