Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/427.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
删除<;预处理>;使用javascript?_Javascript_Jquery_Html_Css_Regex - Fatal编程技术网

删除<;预处理>;使用javascript?

删除<;预处理>;使用javascript?,javascript,jquery,html,css,regex,Javascript,Jquery,Html,Css,Regex,如何在下面编写html代码的所有格式都能得到与图片中相同的结果? HTML: JS (函数(){ var pre=document.getElementsByTagName('pre'), pl=预处理长度; 对于(变量i=0;i代码> 标签的内容吗?' <pre><code>The text line 1 The text line 2 The text line 3</code></pre> <pre><code>

如何在下面编写html代码的所有格式都能得到与图片中相同的结果?

HTML:

JS

(函数(){
var pre=document.getElementsByTagName('pre'),
pl=预处理长度;
对于(变量i=0;i
有关代码详细信息,请访问


底线如果第一行或最后一行为空,则不会显示为已编号。在中间有空行不是问题。

< P>你不能只在代码行之前修剪<代码> >代码> <代码>标签的内容吗?'

<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>

如果代码包含HTML,只需将其作为文本返回即可,因为
pre
标记无论如何都会将HTML显示为文本


预标记保留行距和换行符,因此要使预标记具有相同的格式,它们必须具有相同的结构,例如:

您以前的html中会有换行符:

文本行1
文本第2行

这是我的意思。非常感谢你的帮助me@DyanaPutry如果您的
code
tag中有html标记,这将不起作用,那么如何才能使此代码在@IsmailRBOUH中正常工作。你有更好的主意吗?呵呵。。我希望你有一个解决办法
(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>';
        }
    }
})();
$('pre code').text(function(_,t) { return $.trim(t) })
$('pre code').text(function(_, t) {return $.trim(this.innerHTML)});
<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>
<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>