【发布时间】:2013-10-08 09:45:23
【问题描述】:
我有一个 2500 字的字符串,需要放入不同的 DIV。我使用text = text.split(' ') 函数检索每个单词,然后填充我的DIVs,直到DIV.scrollHeight 高于DIV.offsetHeight,然后它转到下一个DIV。
我遇到的问题是我的 2500 字字符串中的 HTML 标记完全搞砸了:
它关闭了我不应该的<b>标签,不显示</b>,不显示该标签的<font部分,不显示</font>,等等。
知道为什么吗?
非常感谢!
编辑:根据要求,这里是整个 jQuery 代码。如有任何错误,请提前原谅我:)
function addText(texte)
{
// var texte = texte.replace('<', '<');
// var texte = texte.replace('>', '>');
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);
【问题讨论】:
-
你能发布你用来生成 Html 的代码,以及 Html 输出吗?
-
完成。 HTML 输出是这样的:
<b>JDLI</b> : Ellen Page, Williem Dafoe, Hans Zimmer… De tels noms n’entretiennent pas l’ambigüité avec la notion de film interactif ? <b>David</b> Cage : Personnellement, je n’ai jamais ressenti d’ambiguïté à ce sujet.其中</b>标签应该在第一行的句尾,Cage之后是第二行。
标签: javascript jquery regex string split