Javascript 转换<;部门>;至<;p>;

Javascript 转换<;部门>;至<;p>;,javascript,jquery,Javascript,Jquery,我有一个container div,它还有几个其他div: <div class='container'> <div>one</div> <div>two</div> <div>three</div> <div>four</div> </div> 但是这样做不会把所有的div都变成ps吗?如何修改它,使其仅针对类容器中的div中的div?谢谢。您不能真正更改标记名,但

我有一个container div,它还有几个其他div:

<div class='container'>
 <div>one</div>
 <div>two</div>
 <div>three</div>
 <div>four</div>
</div>

但是这样做不会把所有的div都变成ps吗?如何修改它,使其仅针对类容器中的div中的div?谢谢。

您不能真正更改标记名,但可以用p替换现有的div:

$('.container').children('div').each(function(e, el) {
    $(el).replaceWith( $('<p />', {text: $(this).text()}) );
});
$('.container')。子项('div')。每个(函数(e,el){
$(el).replace为($('

',{text:$(this.text()})); });

$(函数(){
$('.container>div')。每个(函数(){
$(this.contents().wrapAll(“”).parent().unwrap();
});
});

如果您按下控制键并打开查找工具,然后单击查找和替换,在查找上键入“
”,在替换上键入“
”,然后查看您想要的内容并点击替换,那么这将很容易完成,不会出现任何错误:)

试试这个

$(".container").children("div").replaceWith(function(){

   return $("<p />").append($(this).contents());

});
$(“.container”).children(“div”).replaceWith(function(){
返回$(“

”).append($(this.contents()); });


我得问问。。。为什么?替换原始HTML源代码,或者对
#container>div
应用一些CSS以添加边距。+1始终存在边距。:)这似乎是更好的方法。。。
  <div class='container'>
   <p>one</p>
   <p>two</p>
   <p>three</p>
   <p>four</p>
  </div>


 <div class='container'>
   <p>one</p>
   <p>two</p>
   <p>three</p>
   <p>four</p>
  </div>
  (function($) {
   $.fn.changeElementType = function(newType) {
    var attrs = {};

    $.each(this[0].attributes, function(idx, attr) {
        attrs[attr.nodeName] = attr.nodeValue;
    });

    this.replaceWith(function() {
        return $(\"<\" + newType + \"/>\", attrs).append($(this).contents());
     });
     }
     })(jQuery);
$(\"div\").changeElementType(\"p\");
$('.container').children('div').each(function(e, el) {
    $(el).replaceWith( $('<p />', {text: $(this).text()}) );
});
$(function() {
    $('.container > div').each(function() {
        $(this).contents().wrapAll("<p>").parent().unwrap();
    });
});
$(".container").children("div").replaceWith(function(){

   return $("<p />").append($(this).contents());

});