我需要我的文本保留幾個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