gpt4 book ai didi

javascript - 给字符串加粗

转载 作者:行者123 更新时间:2023-12-03 02:19:09 25 4
gpt4 key购买 nike

我有一个带有线条的简单跨度。我只想将包含 Running test: 的行加粗。 所有线路

<span id="results" style="white-space: pre-line">Running test: test1
asasggsa
fasfs
afasaggas
</span>

知道怎么做吗?

应该是这样的

 <span id="results" style="white-space: pre-line"><strong>Running test: test1</strong>
asasggsa
fasfs
afasaggas
</span>

最佳答案

使用拆分合并

演示

var value = results.innerHTML; //since results is an id
var textToBold = value.split( "\n" )[0]; //get the first line
results.innerHTML = value.split( textToBold ).join( "<strong>" + textToBold + "</strong>" ); //split by the text to bold and then join with enclosing tags
<span id="results" style="white-space: pre-line">Running test: test1
asasggsa
fasfs
afasaggas
</span>

I just want to bold the line that contains Running test:. All line

在这种情况下使用匹配

演示

var value = results.innerHTML; //since results is an id
var text = "Running test";
var linesToBold = value.split( "\n" ).map( s => s.includes( text ) ? "<strong>" + s + "</strong>" : s ).join( "\n" );
results.innerHTML = linesToBold
<span id="results" style="white-space: pre-line">Running test: test1
asasggsa
fasfs
afasaggas
</span>

关于javascript - 给字符串加粗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49237056/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com