gpt4 book ai didi

javascript - createRange 在 Chrome 中不起作用

转载 作者:行者123 更新时间:2023-11-28 09:36:46 26 4
gpt4 key购买 nike

这适用于 IE9,但不适用于 Chrome。我想在 Chrome 中运行它。这是当我将单词放入文本框中时找到我要查找的单词。谁能帮我解决这个问题吗?

var n = 0;
var str = '';

function chk() {
str = form1.kts.value;
if (str == '') alert('none!');
else ok();
}

function ok() {
var found;
var text = document.body.createTextRange();

for (i = 0; i <= n && (found = text.findText(str)) != false; i++) {
text.moveStart("character", 1);
text.moveEnd("textedit");
}
if (found) {
text.moveStart("character", - 1);
text.findText(str);
text.scrollIntoView();
text.select();
n++;
} else {
if (n > 0) {
n = 0;
ok();
} else {
alert("nothing.");
form1.kts.value = '';
}
}
}

最佳答案

createTextRange 仅在 IE(和某些版本的 Opera)中受支持,要解决您的问题,您可能应该使用 createRange 而不是其他浏览器。

var n = 0,
str = '';

function chk(){
str = form1.kts.value;
if(str == '') {
alert('none!');
}else{
ok();
}
}

function ok(){
var found, text;
if (document.createRange) {
text = document.body.createTextRange();
// do stuff for IE here
}else{
if( document.selectionStart ) {
text = document.setSelectionRange(start, end);
// do stuff for other browsers here
}
}

.........​

关于javascript - createRange 在 Chrome 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12911793/

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