简体   繁体   English

HTML-如何插入 <span></span> 标记到每行 <pre></pre> 块没有硬编码?

[英]HTML - How do I insert a <span></span> tag into each line of a <pre></pre> block without hard coding?

I was just trying to add line numbers at the beginning of source code using CSS. 我只是尝试使用CSS在源代码的开头添加行号。 I realized the effect I wanted, as follows: 我意识到我想要的效果,如下所示:

实施效果

However, the HTML code required continual use of <span>...</span> tags: 但是,HTML代码需要持续使用<span>...</span>标签:

<pre class="code">
  <span>var links = document.getElementsByClassName("link");</span>
  <span>for(var i = 0; i &lt; links.length; i++){</span>
  <span>  links[i].onclick=function(){</span>
  <span>   alert(i+1);</span>
  <span>  };</span>
  <span>}</span>
</pre>

With the span tags positioned at home/end of lines I can let the line numbers show as expected. 通过将span标签放置在行的起始/结尾,我可以让行号按预期显示。 But I think there must be another better solution to prevent me adding all these span tags hard-coded, maybe using Javascript, or jQuery I don't mind but don't know how. 但是我认为必须有另一个更好的解决方案,以防止我添加所有这些span标签进行硬编码,也许使用Javascript或jQuery,但我不介意但不知道如何。 Please help. 请帮忙。

NOTE: 注意:
My problem is not how to display line numbers when the <span> tags are already there. 我的问题不是<span>标记已经存在时如何显示行号。 Instead, I wanted to know if the origin HTML code contains NO <span> tags, how can I automatically add them into the suitable places and so I can apply the CSS styles. 相反,我想知道原始HTML代码是否不包含<span>标记,如何将它们自动添加到合适的位置,以便可以应用CSS样式。

This can be achieved by using CSS counters 这可以通过使用CSS计数器来实现

This does not require any JavaScript (or jQuery) which means no need for each libraries or scripts and was introduced way back in CSS 2.1 so has great browser support across the board. 这不需要任何JavaScript(或jQuery),这意味着不需要每个库或脚本,并且在CSS 2.1中就引入了它,因此对所有浏览器都提供了强大的支持。

 pre { background: #eee; counter-reset: section; /* Reset the counter to 0 for each new pre */ } pre span:before { counter-increment: section; /* Increment the section counter*/ content: counter(section); /* Display the counter */ padding: 0 5px; border-right: 1px solid #777; margin-right: 5px; color: #777 } 
 <pre class="code"> <span>var links = document.getElementsByClassName("link");</span> <span>for(var i = 0; i &lt; links.length; i++){</span> <span> links[i].onclick=function(){</span> <span> alert(i+1);</span> <span> };</span> <span>}</span> </pre> <pre class="code"> <span>var links = document.getElementsByClassName("link");</span> <span>for(var i = 0; i &lt; links.length; i++){</span> <span> links[i].onclick=function(){</span> <span> alert(i+1);</span> <span> };</span> <span>}</span> </pre> 

So you basically need to append a span before each line. 因此,您基本上需要在每行之前附加一个span Here is the codepen link . 这是codepen链接 I am currently using jQuery's insertBefore() method for it. 我目前正在使用jQuery's insertBefore()方法。

See below for the explanation of the code :- $('.code') will give you the pre tag . 有关代码的说明,请参见下文:- $('.code')将为您提供pre tag Now the jQuery .find() method will give you all the spans inside the pre tag. 现在,jQuery .find()方法将为您提供pre标签内的所有跨度。 Now jQuery .each() function is basically a for loop ( in simple terms ) which will loop for all the span tags inside the pre tag. 现在,jQuery .each()函数基本上a for loop (简单而言),它将为pre标记内的所有span标记循环。 .insertBefore() function simply inserts whatever there is in the selector to the element mentioned inside the function . .insertBefore()函数只是将选择器中的所有内容插入到inside the function提到的元素中。

 var iCount = 1; $('.code').find('span').each(function(){ $( "<span>"+iCount+"| </span>" ).insertBefore( this); iCount++; }); 
 pre{ background: #eee } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <pre class="code"> <span>var links = document.getElementsByClassName("link");</span> <span>for(var i = 0; i &lt; links.length; i++){</span> <span> links[i].onclick=function(){</span> <span> alert(i+1);</span> <span> };</span> <span>}</span> </pre> 

I have combined @Stewartside answer with what you have actually asked for. 我已将@Stewartside答案与您实际要求的内容结合在一起。

Below you can see a simple plain JavaScript to replace any line in element with code class to be wrapped in span which applies @Stewartside css. 在下面,您可以看到一个简单的普通JavaScript,它将元素中的任何行替换为使用@Stewartside css封装在span中的 代码类。

 var codeElement = document.getElementsByClassName("code"); //array of code blocks var formattedCode = codeElement[0].textContent.replace("\\r\\n", "\\n").split("\\n"); var codeLength = formattedCode.length; formattedCode.forEach(function(line, index, array) { if (codeLength - 1 == index) return; array[index] = "<span>" + line + "</span>"; }); codeElement[0].innerHTML = formattedCode.join("\\n"); $(".code-jquery").each(function(index, codeElement) { var formattedCode = $(codeElement).html().replace("\\r\\n", "\\n").split("\\n"); var codeLength = formattedCode.length; $(codeElement).text(""); $.each(formattedCode, function(index, line) { if (codeLength - 1 == index) return; $(codeElement).append("<span>" + line + "</span>\\n") }); }); 
 pre { background: #eee; counter-reset: section; /* Reset the counter to 0 for each new pre */ } pre span:before { counter-increment: section; /* Increment the section counter*/ content: counter(section); /* Display the counter */ padding: 0 5px; border-right: 1px solid #777; margin-right: 5px; color: #777 } pre.code-jquery span { color: green; } 
 <pre class="code"> var links = document.getElementsByClassName("link"); for(var i = 0; i &lt; links.length; i++) { links[i].onclick = function() { alert(i+1); }; } </pre> //jQuery version <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <pre class="code-jquery"> var links = document.getElementsByClassName("link"); for(var i = 0; i &lt; links.length; i++) { links[i].onclick = function() { alert(i+1); }; } </pre> 

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

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