简体   繁体   English

jQuery:如何选择所有特定于div的类并将div中的所有内容附加到新的div

[英]jQuery: How to select all divs specific class and append everything inside the div to a new div

I am building a style guide for a application. 我正在为应用程序构建样式指南。 I have several examples of different widgets. 我有几个不同小部件的示例。 I have wrapped each html block that I want to use as a snippet with the class "snippet". 我已经将每个要用作代码段的html块包装为“代码段”类。 I have a second empty div below each example widget with the class "example". 我在每个示例小部件下方都有一个第二个空div,其类为“ example”。 I am trying to use jQuery to select all the html blocks with the code snippet, grab everything inside the div and insert it inside the empty div (wrapped in pre and code tags). 我正在尝试使用jQuery选择带有代码段的所有html块,抓取div中的所有内容并将其插入空div(包装在pre和code标签中)。

Please see below for example: 请参见以下示例:

<div class="panel panel-default">
    <div class="snippet panel-body">
        <h1>H1 Header Example</h1>
        <h2>H2 Header Example</h2>
        <h3>H3 Header Example</h3>
        <h4>H4 Header Example</h4>
        <p>
            Here is a p tag with a <a href="#">Link</a>
        </p>
    </div>
</div>
<div class="example">
</div>

have a second empty div below each example widget with the class "example". 在每个示例小部件下方都有一个第二个空div,其类别为“ example”。 I am trying to use jQuery to select all the html blocks with the code snippet, grab everything inside the div and insert it inside the empty div (wrapped in pre and code tags). 我正在尝试使用jQuery选择带有代码段的所有html块,抓取div中的所有内容并将其插入空div(包装在pre和code标签中)。

If interpret Question correctly , try using .each() , .innerHTML , textarea element 如果正确地解释问题,请尝试使用.each() .innerHTMLtextarea元素

 $(".example").each(function(i, el) { el.innerHTML = "<textarea style=width:400px;height:200px>" + $(".snippet")[i].innerHTML + "</textarea>" }) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"> </script> <div class="panel panel-default"> <div class="snippet panel-body"> <h1>H1 Header Example</h1> <h2>H2 Header Example</h2> <h3>H3 Header Example</h3> <h4>H4 Header Example</h4> <p> Here is ap tag with a <a href="#">Link</a> </p> </div> </div> <div class="example"> </div> 


If rendered html is not to be edited , could add disabled attribute to string which renders textarea element 如果渲染的html不被编辑,则可以将disabled属性添加到渲染textarea元素的字符串中

 $(".example").each(function(i, el) { el.innerHTML = "<textarea style=width:400px;height:200px disabled=true>" + $(".snippet")[i].outerHTML + "</textarea>" }) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"> </script> <div class="panel panel-default"> <div class="snippet panel-body"> <h1>H1 Header Example</h1> <h2>H2 Header Example</h2> <h3>H3 Header Example</h3> <h4>H4 Header Example</h4> <p> Here is ap tag with a <a href="#">Link</a> </p> </div> </div> <div class="example"> </div> 

The reason you are getting this error is because [0] returns the plain Dom object not a jquery one. 您收到此错误的原因是因为[0]返回普通的Dom对象而不是jquery对象。

$('.snippet').each(function(i, item){ 
   $(item).next().text($(item).html());
  }
);

使用$('.example').html($('.snippet').html());

Did you mean something like this? 你的意思是这样吗?

 $(".example").append("<pre><code/></pre>"); $(".example code").html($(".snippet").html().replace(/</g, "&lt;").replace(/>/g, "&gt;")); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div class="panel panel-default"> <div class="snippet panel-body"> <h1>H1 Header Example</h1> <h2>H2 Header Example</h2> <h3>H3 Header Example</h3> <h4>H4 Header Example</h4> <p> Here is ap tag with a <a href="#">Link</a> </p> </div> </div> <div class="example"> </div> 


More elaborately, you could make this more dynamic: generate .example after each snippet and fill it with the code from the preceding snippet. 更详细地说,您可以使其更加动态:在每个代码段之后生成.example ,并使用前面代码段中的代码填充它。

 $(".snippet").each(function() { var $this = $(this), ex = $("<div></div>").addClass("example").html(function() { return "<pre><code>" + $this.html().replace(/</g, "&lt;").replace(/>/g, "&gt;") + "</pre></code>"; }); $this.parent("div").after(ex); }); 
 .example { background: #ccc; text-shadow: 0 1px white; } .example pre {padding: 0;} 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="panel panel-default"> <div class="snippet panel-body"> <h1>H1 Header Example</h1> <h2>H2 Header Example</h2> <h3>H3 Header Example</h3> <h4>H4 Header Example</h4> <p> Here is ap tag with a <a href="#">Link</a> </p> </div> </div> <div class="panel panel-default"> <div class="snippet panel-body"> <h1>H1 Another Header Example</h1> <h2>H2 Something similarHeader Example</h2> <h3>H3 Bananas Header Example</h3> <p> Here is ap tag with a <a href="#">Link</a> and guess what, even a <textarea disabled>I like turtles</textarea> </p> </div> </div> 

At the top of your html file add this link 在您的html文件顶部添加此链接

  <script src="https://code.jquery.com/jquery-1.10.2.js"></script> 

And at the bottom at this code to copy the div content and put it in ".example" div 并在此代码的底部复制div内容并将其放在“ .example” div中

 <script>
  $(document).ready(function () {  
     $('.example').html($('.snippet').clone()); 
    //$('.panel  .snippet').remove(); // use this portion to remove the previous div
   });
</script> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM