2012-04-20 73 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