2012-04-20 76 views
1

我不知道例程setSelectionRange文本框在新的Firefox夏天不起作用。在MDN站点(Mozilla开发中心)将其指定:XUL文本框setSelectionRange

setSelectionRange(开始,结束) 返回类型:无 设置的文本框,其中,所述开始参数是的索引的选择的部分的返回值要选择的第一个字符和结束参数是选择后字符的索引 。将两个参数都设置为 具有相同的值,以将光标移动到相应位置 而不选择文本。

<?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> 
+1

https://developer.mozilla.org/en/XUL_Tutorial/Focus_and_Selection – linguini 2012-04-20 06:07:11

回答

1

功能工作得很好。但是,当您单击一个按钮时,输入焦点(在逻辑上)在按钮上,并且setSelectionRange不会改变它。您可以按选项卡选择文本字段并使选择可见。或者,你也可以将此行添加到您sel()功能焦点的文本字段:

textbox.focus(); 
+0

谢谢,工作就像我想要的。 – user916933 2012-04-21 08:27:13

+0

@ user916933:随意接受答案,如果它的工作:http://stackoverflow.com/faq#howtoask – 2012-04-21 09:06:58

+0

将有可能选择一个句子的多个部分? – user916933 2012-04-21 14:20:28