我需要我的文本保留几个html格式标记,以便随着文本的变化改变文本的颜色,并显示放置在我的文本中的一些图形图标。我的代码从现有的隐藏段落中获取文本,使段落空白,取消隐藏,然后逐字显示如下。以字母形式显示文本但保留html格式
var NextHighlight = HighlightsTop.find('.newhidden').first().closest('p'); // Find the paragraph.
// Put the text from this highlight paragraph into a variable and remove it from the paragraph, so that we can put it back letter-by-letter below.
var HighlightText = NextHighlight.html();
NextHighlight.html("");
NextHighlight.removeClass('newhidden');
// Add the text back in, letter by letter.
showText(NextHighlight, HighlightText, 0, 5);
....
// The letter-by-letter function.
var showText = function (target, message, index, interval) {
if (index < message.length) {
$(target).append(message[index++]);
setTimeout(function() { showText(target, message, index, interval); }, interval);
}
}
问题是html标签现在只显示为直线文本。如果我在元素中将'html'更改为'text',它不会显示标签,但我也没有获得格式。
编辑 - 下面UPDATE ....
与下面的答案更新后,我的代码现在看起来是这样。我应该指出,当用户点击一个按钮时会发生这种情况,所以我从点击按钮开始添加代码。也许在我看不到的地方有一个错误,因为当我现在单击按钮时,我什么也没有得到。我在控制台中也没有看到任何错误,想知道我是否错过了一次;某处...
$(document).on('click', '.highlight_button',
function() {
var MatchReportTop = $(this).closest('.match_div');
var HighlightsTop = $(this).closest('.highlights');
var NextHighlight = $('.newhidden').first().closest('p'), HighlightTextNodes = [];
// recursively get the text nodes
(function recursiveGetTextNodes(node) {
if(node.nodeType === 3) {
// we're a text node? great, store it. We'll store a clone and the original so we don't lose our place.
HighlightTextNodes.push({
active: node,
ref: node.cloneNode()
});
// clear out the original.
node.textContent = '';
} else {
// no text node. go deeper.
for(var i = 0, len = node.childNodes.length; i < len; i++) {
recursiveGetTextNodes(node.childNodes[i]);
}
}
})(NextHighlight.get(0))
// Add the text back in, letter by letter.
showText(HighlightTextNodes, 5);
if (NextHighlight.hasClass('FullTime')) {
CompleteReveal(MatchReportTop);
}
});
// The letter-by-letter function.
function showText(target, interval) {
// to know what we're currently working on, we need to filter
// the text nodes by a check to see if they are already fully
// "typed" out. If the content doesn't match, keep it. Also,
// `shift()` off the first one. That's the one we'll edit
// this go around.
var current = target.filter(function(a){
return a.active.textContent != a.ref.textContent;
}).shift();
// if we have a node to work with, the process is not
// completed. Once `current` is false, we know we've completed
// the process.
if (current) {
// grab the reference node's text up to the active node's
// length +1 to get the current text plus one letter.
current.active.textContent = current.ref.textContent.substr(0,current.active.textContent.length + 1);
// rinse and repeat.
setTimeout(function() {
showText(target, interval);
}, interval);
}
}
有没有可能看到标记的一小部分?我不太了解'.highlight_button','.match_div','.ighighlights'等在布局中的位置。 –
这是一个标记的例子,虽然现在,我不认为这是问题,因为当我按下按钮时,什么也没有发生。它的反应就好像有一个错误,它只是停止。无论如何,这是一个线的例子.... 2:01 - 斯蒂芬林克特灵巧地跳过了雷Whaley并从盒子内射门射门。迦勒海登潜入对方,但无法到达它,它飞入网的屋顶。 目标!阿灵顿流浪者0:1艾尔斯福德竞技 – Farflame
.highlight_button是按钮本身的类。 .match_div只是一个包含许多文本行的div。我正在做的是,每次用户点击一个按钮时,都会出现另一行文本。我可以给你一个链接/传递给游戏本身,所以你可以看看它的行动,也许在控制台中通过它。 但我仍然认为代码有些问题,因为它只是无所作为(例如,我认为我错过了一个或某个支架)。 – Farflame