createRange在Chrome中无法使用 [英] createRange does not work in Chrome
本文介绍了createRange在Chrome中无法使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这适用于IE9,但不适用于Chrome。
我想在Chrome中运行它。
这就是当我把这个单词放在文本框中时,找到我正在寻找的单词。
任何人都可以为我解决这个问题??
var n = 0;
var str ='';
函数chk(){
str = form1.kts.value;
if(str =='')alert('none!');
else ok();
}
函数ok(){
var found;
var text = document.body.createTextRange(); (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 ='';
解决方案
createRange $ c>来支持 var n = 0,
str ='';
函数chk(){
str = form1.kts.value;
if(str ==''){
alert('none!');
} else {
ok();
函数ok(){
var found,text;
if(document.createRange){
text = document.body.createTextRange();
//在这里为IE做些东西
} else {
if(document.selectionStart){
text = document.setSelectionRange(start,end);
//为其他浏览器做些事
$ b .........
$ / code >
This works in IE9, but not in Chrome. I want to run this in Chrome. this is to find the word i am looking for when i put the word in Textbox. Could anyone solve this problem for me??
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
is only supported in IE (and some versions of Opera), to fix your problem you should probably use createRange
instead for other browsers.
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
}
}
.........
这篇关于createRange在Chrome中无法使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文