admin管理员组文章数量:1415664
How all formats of writing html code below can be issued the same result as in the picture?
HTML:
<pre><code>The text line 1
The text line 2
The text line 3</code></pre>
<pre><code>The text line 1
The text line 2
The text line 3
</code></pre>
<pre><code>
The text line 1
The text line 2
The text line 3
</code></pre>
JS
(function() {
var pre = document.getElementsByTagName('pre'),
pl = pre.length;
for (var i = 0; i < pl; i++) {
pre[i].innerHTML = '<span class="line-number"></span>' + pre[i].innerHTML + '<span class="cl"></span>';
var num = pre[i].innerHTML.split(/\n/).length;
for (var j = 0; j < num; j++) {
var line_num = pre[i].getElementsByTagName('span')[0];
line_num.innerHTML += '<span>' + (j+1) + '</span>';
}
}
})();
Code details can be seen at /
Bottom line if the first line or the last row is empty it will not appear numbered. Not a problem if there is a blank line in the middle.
How all formats of writing html code below can be issued the same result as in the picture?
HTML:
<pre><code>The text line 1
The text line 2
The text line 3</code></pre>
<pre><code>The text line 1
The text line 2
The text line 3
</code></pre>
<pre><code>
The text line 1
The text line 2
The text line 3
</code></pre>
JS
(function() {
var pre = document.getElementsByTagName('pre'),
pl = pre.length;
for (var i = 0; i < pl; i++) {
pre[i].innerHTML = '<span class="line-number"></span>' + pre[i].innerHTML + '<span class="cl"></span>';
var num = pre[i].innerHTML.split(/\n/).length;
for (var j = 0; j < num; j++) {
var line_num = pre[i].getElementsByTagName('span')[0];
line_num.innerHTML += '<span>' + (j+1) + '</span>';
}
}
})();
Code details can be seen at http://jsfiddle/hidyanaputri/sqq2qkon/1/
Bottom line if the first line or the last row is empty it will not appear numbered. Not a problem if there is a blank line in the middle.
Share Improve this question asked Jul 28, 2016 at 20:18 Dyana PutryDyana Putry 1071 silver badge8 bronze badges2 Answers
Reset to default 7Couldn't you just trim the content of the <code>
tags before you number the lines?`
$('pre code').text(function(_,t) { return $.trim(t) })
FIDDLE
It the code contains HTML, just return that as the text instead, as pre
tags will display the HTML as text anyway
$('pre code').text(function(_, t) {return $.trim(this.innerHTML)});
FIDDLE
The pre tag preserves both line spaces and breaks, so to get the pre tags all the same format, they will have to have the same stucture eg:
<pre><code>The text line 1
The text line 2
The text line 3</code></pre>
<pre><code>The text line 1
The text line 2
The text line 3</code></pre>
<pre><code>The text line 1
The text line 2
The text line 3</code></pre>
Your previous html would have line breaks in it:
<pre><code>The text line 1
The text line 2
The text line 3</code></pre>
<pre><code>The text line 1
The text line 2
The text line 3<-- line break here
</code></pre>
<pre><code><-- line break here
The text line 1
The text line 2
The text line 3<-- line break here
</code></pre>
本文标签: jqueryRemoving blank lines in ltpregt with javascriptStack Overflow
版权声明:本文标题:jquery - Removing blank lines in <pre> with javascript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745208844a2647768.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论