可能なすべての方法を試しましたが、変数に格納されて特定の関数内で使用される文字列内にブレークラインを追加することはできません。
HTMLコード:
<body>
<p id="msg"></p>
</body>
Jqueryコード:
<script>
var showText = function (target, message, index, interval) {
if (index < message.length) {
$(target).append(message[index++]);
setTimeout(function () { showText(target, message, index, interval); }, interval);
}
}
var x = "Hello there stranger!"+"<br>"+"How are you?";
$(function () {
showText("#msg", x, 0, 130);
});
</script>
<br>
ブレークラインを追加する代わりに、タグを文字列として出力します。助けてください...
あなたのポイントは:
[jQuery.append][1]
の代わりに:
[jQuery.html][2]
私はあなたに提案します:
var showText = function (target, message, index, interval) {
if (index < message.length) {
// if special character: append the break element
if (message[index] == '\n') {
index++;
$(target).append('<br/>');
} else {
// else: append text to the html
$(target).html($(target).html() + message[index++]);
}
setTimeout(function () {
showText(target, message, index, interval);
}, interval);
}
}
// use a special character instead of the break element
var x = "Hello there stranger!\nHow are you?";
$(function () {
showText("#msg", x, 0, 130);
});
<script src="https://code.jquery.com/jquery-1.12.1.min.js"></script>
<p id="msg"></p>
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加