简体   繁体   English

JavaScript split()弄乱了我的html标签

[英]Javascript split() messes up my html tags

I have a 2500-words string that I need to be put in different DIV . 我有一个2500字的字符串,需要将其放入不同的DIV I have used the text = text.split(' ') function to retrieve each word and then have my DIVs filled until DIV.scrollHeight becomes higher than DIV.offsetHeight , then it goes to the next DIV . 我使用text = text.split(' ')函数检索每个单词,然后填充我的DIVs直到DIV.scrollHeight变得高于DIV.offsetHeight ,然后转到下一个DIV

The problem I encounter is that my HTML tags in my 2500-word string are completely messed up: it closes my <b> tags where it shouldn't, doesn't display the </b> where it should, doesn't display the <font part of this tag, doesn't display the </font> , etc. 我遇到的问题是我的2500个单词的字符串中的HTML标记被完全弄乱了:它将<b>标记关闭在不应该显示的位置,不显示</b>应当显示的位置,不显示此标记的<font部分,不显示</font>等。

Any idea why? 知道为什么吗?

Many thanks! 非常感谢!

Edit: As requested, here is the entire jQuery code. 编辑:根据要求,这里是完整的jQuery代码。 Pardon me in advance for any mistake in it :) 事先请原谅我任何错误:)

function addText(texte)
   {
        // var texte = texte.replace('<', '&lt;');
        // var texte = texte.replace('>', '&gt;');
        var container = $('DIV[id^=apDiv]').find('DIV#bloc_prim');
        console.log('NOMBRE DE CONTAINER : '+container.length);
        var length = texte.length;
        console.log('LONGUEUR DU TEXTE '+length+' caractères');
        // container.html(texte);
        var hauteurDiv = container.prop('offsetHeight');
        var hauteurContent = container.prop('scrollHeight');
        length = Math.floor((length * hauteurDiv)/hauteurContent);

        console.log(hauteurContent);
        // container.html(texte.substring(0, length));
        var hauteurRestante = hauteurContent - hauteurDiv;

        console.log(hauteurRestante);

        var TABLEAU = texte.split(' ');
        // var TABLEAU = texte.match(/<\s*(\w+\b)(?:(?!<\s*\/\s*\1\b)[\s\S])*<\s*\/\s*\1\s*>|\S+/g);
        console.log('LONGUEUR TABLEAU : '+TABLEAU.length+' occurences');
        // console.log(TABLEAU[4]);
        i = 0;
        hauteurTotale = container.prop('scrollHeight');
        console.log(container.prop('offsetHeight'));
        console.log(hauteurTotale);
        while(i < TABLEAU.length)
        {

            while(hauteurTotale <= container.prop('offsetHeight'))
            {
                container.append(TABLEAU[i]+' ');
                i++;
                hauteurTotale = container.prop('scrollHeight');
                console.log(i+':'+hauteurTotale+' --- '+container.prop('offsetHeight'));
            }
            if(i < TABLEAU.length)
            {
                var clone = container.parent('DIV[id^=apDiv]').clone(true); // On copie la DIV
                $('BODY').append(clone); // On colle la partie copiée
                clone.find('DIV#bloc_prim').empty();
            }
            var hauteurTotale = clone.find('DIV#bloc_prim').prop('scrollHeight');
            console.log(hauteurTotale);
            while(hauteurTotale <= clone.find('DIV#bloc_prim').prop('offsetHeight'))
            {
                if(i < TABLEAU.length)
                {
                    clone.find('DIV#bloc_prim').append(TABLEAU[i]+' ');
                    i++;
                    hauteurTotale = clone.find('DIV#bloc_prim').prop('scrollHeight');
                    console.log(i+':'+hauteurTotale+' --- '+container.prop('offsetHeight'));
                }
                else
                {
                    break;
                }

            }
        }
        console.log(i+'->'+TABLEAU.length);

I think the problem could be that you might be splitting tags and then the browser tries to fix it messing it all up. 我认为问题可能是您可能正在拆分标签,然后浏览器试图对其进行修复以将其弄乱。

With you algorithm you might end up with something like: 使用您的算法,您可能最终会遇到以下情况:

<div>
    <p>The text starts here
</div>
<div>
    and ends here</p>
</div>

Which is clearly not the best thing. 显然这不是最好的事情。 I think you should keep track of the opening/closing tags and only change container div when they're all closed 我认为您应该跟踪打开/关闭标签,仅在它们全部关闭时更改容器div

I have solved the problem. 我已经解决了问题。 I used the following Regex to catch the words and HTML tags and used the following .replace() to add spaces after all my <br/> (which was the source of the problem). 我使用以下正则表达式捕获单词和HTML标记,并使用以下.replace()在所有<br/>之后添加空格(这是问题的根源)。

var texte = texte.replace(/<br>/g, '<br/> '); var texte = texte.replace(/<br>/g, '<br/> ');

var TABLEAU = texte.match(/<\\s*(\\w+\\b)(?:(?!<\\s*\\/\\s*\\1\\b)[\\s\\S])*<\\s*\\/\\s*\\1\\s*>|\\S+/gi); var TABLEAU = texte.match(/<\\s*(\\w+\\b)(?:(?!<\\s*\\/\\s*\\1\\b)[\\s\\S])*<\\s*\\/\\s*\\1\\s*>|\\S+/gi);

This did the trick. 这成功了。

Thanks all! 谢谢大家!

If you want to treat tag contents as single words you can try instead of var TABLEAU = texte.split(' '); 如果要将标签内容视为单个单词,可以尝试代替var TABLEAU = texte.split(' '); to use 使用

var nodes = $('<div/>',{ html: texte }).contents();
var TABLEAU = nodes.map(function(){
    return this.nodeType === 3 ? $(this).text().split(' ') : this;
}).get();

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

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