我使用jQuery在游戏结束后更改h1标题,我有以下代码行:

$('h1').text(winningPlayer+" has won!").css("fontSize", "55px");

问题是,如何为文本的winningPlayer部分设置不同的颜色??

最佳答案

winningPlayer包入<span>并将.text()更改为.html()。下面是演示

let winningPlayer = "John"
$('h1').html(`<span style="color:blue">${winningPlayer}</span> has won!`).css("fontSize", "55px");

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<h1></h1>

08-25 16:05