[英]How do I print HTML code with indentantion?
I'm creating a layout page with the components that I'm using in a project for other people use the same design. 我正在使用我在项目中使用的组件创建布局页面,以供其他人使用相同的设计。 My ideia is to let my script get my innerHTML code and print it for the user as a text;
我的想法是让我的脚本获取我的innerHTML代码,并以文本形式为用户打印;
Here's the codepen . 这是codepen 。
So far, I have this: 到目前为止,我有这个:
// Get all divs with the ".topic" class and returns a list of it // @return list | array() function getTopicList() { // Create the list let list = []; // Populate the list $('.topic').map(function() { list.push($(this)); }); // Returns the list return list; } // Set the example code inside a "code" tag // @param topic | jquery object // @param exampleCode | string function setExampleCode(topic, exampleCode) { // Create a "code" tag inside the selected div $(topic).append('<code></code>'); // Set the example's code inside the "code" tag $(topic).find('code').text(exampleCode); } // Get the HTML code inside the div as a string // @param topic | jquery object function generateExampleCode(topic) { // Get the HTML code as string let exampleCode = $(topic).children('.example').html(); // Calls a function to append the code as a string into the div setExampleCode(topic, exampleCode); } $(document).ready(function() { // Get a list of all the divs that have the ".topic" class let topicList = getTopicList(); // Get the HTML code of all the ".topic" divs topicList.map(generateExampleCode); });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <!-- Buttons --> <div class="topic white z-depth-1 u-padding-20"> <h5>Buttons</h5> <p class="u-margin-20-0">Materialize buttons.</p> <div class="example grey lighten-3 u-padding-20 u-margin-20-0 u-overflow-hidden u-box-shadow-inset-1"> <button class="btn left">Button</button> <button class="btn left"><i class="material-icons left">add</i>Button</button> <button class="btn left"><i class="material-icons right">add</i>Button</button> </div> </div>
And my result is the following: 我的结果如下:
My expected result is: 我的预期结果是:
I know that I can manipulate the string and replace the "<" for " <
" and the ">" for " >
", but is it the only way to do it? 我知道我可以操纵字符串并用“ <”代替“
<
”,用“>”代替“ >
”,但这是唯一的方法吗? Is there a simpler way to do it? 有更简单的方法吗?
would it not be appropriate to use "pre" tag like. 使用“ pre”标签之类的方法不合适吗?
function setExampleCode(topic, exampleCode) {
// Create a "code" tag inside the selected div
$(topic).append('<code><pre></pre></code>');
// Set the example's code inside the "code" tag
$(topic).find('pre').text(exampleCode);
}
Maybe all you need is some CSS? 也许您只需要一些CSS?
code {
white-space: pre-line;
}
You can use the "pre" element when displaying text with unusual formatting, or some sort of computer code. 当显示格式不正常的文本或某种计算机代码时,可以使用“ pre”元素。 HTML pre Tag
HTML pre标签
// Get all divs with the ".topic" class and returns a list of it // @return list | array() function getTopicList() { // Create the list let list = []; // Populate the list $('.topic').map(function() { list.push($(this)); }); // Returns the list return list; } // Set the example code inside a "code" tag // @param topic | jquery object // @param exampleCode | string function setExampleCode(topic, exampleCode) { // Create a "code" tag inside the selected div $(topic).append('<pre></pre>'); // Set the example's code inside the "code" tag $(topic).find('pre').text((exampleCode)); } // Get the HTML code inside the div as a string // @param topic | jquery object function generateExampleCode(topic) { // Get the HTML code as string let exampleCode = $(topic).children('.example').html(); // Calls a function to append the code as a string into the div setExampleCode(topic, exampleCode); } $(document).ready(function() { // Get a list of all the divs that have the ".topic" class let topicList = getTopicList(); // Get the HTML code of all the ".topic" divs topicList.map(generateExampleCode); });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <!-- Buttons --> <div class="topic white z-depth-1 u-padding-20"> <h5>Buttons</h5> <p class="u-margin-20-0">Materialize buttons.</p> <div class="example grey lighten-3 u-padding-20 u-margin-20-0 u-overflow-hidden u-box-shadow-inset-1"> <button class="btn left">Button</button> <button class="btn left"><i class="material-icons left">add</i>Button</button> <button class="btn left"><i class="material-icons right">add</i>Button</button> </div> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.