gpt4 book ai didi

Javascript document.all 和 document.getSelection - Firefox 替代品

转载 作者:行者123 更新时间:2023-11-30 13:43:45 25 4
gpt4 key购买 nike

下面的脚本将替换文本区域中的选定单词。但它只适用于 IE。知道如何让它在 Firefox 上运行吗? (问题似乎出在 (document.all)?document.selection.createRange() : document.getSelection();)

<SCRIPT LANGUAGE="JavaScript">
<!--//
var seltext = null;
var repltext = null;
function replaceit()
{
seltext = (document.all)? document.selection.createRange() : document.getSelection();
var selit = (document.all)? document.selection.createRange().text : document.getSelection();
if (selit.length>=1){
if (seltext) {
repltext= prompt('Please enter the word to replace:', ' ');
if ((repltext==' ')||(repltext==null)) repltext=seltext.text;
seltext.text = repltext;
window.focus()
}
}
}
//-->
</SCRIPT>
</HEAD>
<BODY>
<form name="f">
<textarea cols='40' rows='10' name='msg'></textarea>

<input type="button" name="b" value="Replace" onClick="replaceit();">
</form>
</BODY>

最佳答案

document.all 位被用作测试以查看它是否为 IE。 document.getSelection() 的写法是在 Firefox 中使用的,在 IE 中是 document.selection.createRange()

http://www.hscripts.com/tutorials/javascript/ternary.php

所以问题不在于 document.all,而在于 getSelection() 不工作。不确定为什么这不是我最近使用的构造,但按照这个尝试 window.getSelection() :(如果这不起作用,请谷歌搜索其他人) http://www.webdeveloper.com/forum/archive/index.php/t-138944.html

关于Javascript document.all 和 document.getSelection - Firefox 替代品,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/749970/

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