1 回答
TA贡献2051条经验 获得超10个赞
您的代码正在尝试设置字符串的innerHTML - 这不起作用
而且 [i] 不存在于循环之外
试试这张地图
const seperateText = str => {
let splittedText = str.split(" ");
return splittedText.map(word => word.length > 4 ? word : "hello").join(" ");
}
let header1 = document.getElementById("h1")
header1.innerHTML = seperateText(header1.innerHTML);
let header2 = document.getElementById("empty")
header2.innerHTML = seperateText(header2.innerHTML);
<h1 id="h1">Please Answer the Questions</h1>
<h1 id="empty"></h1>
或者这个替换
const seperateText = str => {
return str.replace(/\b(\w+)\b/gi, match => match.length < 4 ? "hello" : match);
}
let header1 = document.getElementById("h1")
header1.innerHTML = seperateText(header1.innerHTML);
let header2 = document.getElementById("empty")
header2.innerHTML = seperateText(header2.innerHTML);
<h1 id="h1">Please Answer the Questions</h1>
<h1 id="empty"></h1>
添加回答
举报