0

これは、コードを実行したときに得られる出力です。

[19:01:06] User: <a href="http://www.google.com" target="_blank" >http://www.google.com </a>

リンクは作成せず、a href タグのみを表示します。

これがリンクを作成するものです:

function linkify(inputText) {
var replacedText, replacePattern1, replacePattern2, replacePattern3;

//URLs starting with http://, https://, or ftp://
replacePattern1 = /(\b(https?|ftp):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gim;
replacedText = inputText.replace(replacePattern1, '<a href="$1" target="_blank">$1</a>');

//URLs starting with "www." (without // before it, or it'd re-link the ones done above).
replacePattern2 = /(^|[^\/])(www\.[\S]+(\b|$))/gim;
replacedText = replacedText.replace(replacePattern2, '$1<a href="http://$2" target="_blank">$2</a>');

return replacedText;

}

そのため、チュートリアルのチャット ボックスで、次のようなテキストを firebase リストに送信しました。

text=linkify(text);
    myDataRef.push({timestamp: timestamp, name: name, text: text, emote: emote});

表示部分はこんな感じです。

function displayChatMessage(timestamp, name, text, emote) {

        $('<div/>').text('['+timestamp+'] ').append($('<name/>').text(name+': ')).append($('<em/>').text(text)).appendTo($('#messagesDiv'));
}
$('#messagesDiv')[0].scrollTop = $('#messagesDiv')[0].scrollHeight;

};

他のhtmlタグを挿入しようとしましたが、それもうまくいきません。コード全体は、チュートリアルのチャットに基づいており、そこから作業しています。

4

1 に答える 1