<textarea class="content"></textarea>
<div class="box">君不见&黄河&之水天上来,奔流到海不&复回&</div>
<button type="button" onclick="determine()">生成</button>
<script>
let distance = []
function determine() {
let value = $('.content').val()
var index = -1; //令index=-1使得indexOf从索引0开始进行遍历。
do {
index = value.indexOf('&', index + 1);
if (index !== -1) {
distance.push(index)
if (distance.length % 2 == 0) {
replacepos(value, distance[0] - 1, distance[1] + 1, '_____');
function replacepos(text, start, stop, replacetext) {
value = text.substring(0, start+1) + replacetext + text.substring(stop);
distance.shift()
distance.shift()
return value;
}
console.log(value);
}
}
} while (index !== -1);
$('.box').html(value);
}
</script>
填空题挖空,替换指定位置字符
最新推荐文章于 2024-08-15 08:55:57 发布