1

我想知道文本框的常规 setSelectionRange 是否在新的 Firefox 夏季中不起作用。在 MDN 站点(Mozilla 开发者中心)中,它被指定为:

setSelectionRange( start, end ) 返回类型:无返回值 设置文本框的选定部分,其中 start 参数是要选择的第一个字符的索引,end 参数是选择后字符的索引。将两个参数设置为相同的值以将光标移动到相应的位置而不选择文本。

<?xml-stylesheet href="chrome://global/skin/" type="text/css"?>
<window id="yourwindow"
        xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
        xmlns:html="http://www.w3.org/1999/xhtml"
        xmlns:h="http://www.w3.org/1999/xhtml">

<button label="x"  oncommand="sel()"  />

<textbox id="id" multiline="true"  
         value="This is some text that could wrap onto multiple lines."/>  

<script type="text/javascript">
<![CDATA[
function sel(){
var textbox = document.getElementById("id");
textbox.setSelectionRange( 1 , 2 );
}
]]>
</script>
</window>
4

1 回答 1

1

The function works just fine. However, when you click a button the input focus is (logically) on the button and setSelectionRange won't change that. You can press Tab to select the text field and make the selection visible. Alternatively, you can also add this line to your sel() function to focus the text field:

textbox.focus();
于 2012-04-20T11:31:07.890 回答