简体   繁体   English

javascript中的Mozilla firefox问题

[英]Mozilla firefox problem in javascript

how i can get (if)any text is selected in textbox and i want to get it in any variable of javascript.... specifically for Mozilla firefox...? 我怎么能(如果)在文本框中选择任何文本,我想在javascript的任何变量中获取它。...专门针对Mozilla firefox ...? Thanks in advance! 提前致谢!

This should work: 这应该工作:

 alert(document.getElementById('TextBoxID').value);

And asigning that value to some variable: 并将该值分配给一些变量:

 var variablename = document.getElementById('TextBoxID').value

Edit: I just saw that you want to read only the selected text. 编辑:我刚刚看到您只想阅读所选的文本。 This can be done this way: 这可以通过以下方式完成:

 if (TextBox.selectionStart != undefined)
  {
    var startPos = TextBox.selectionStart;
    var endPos = TextBox.selectionEnd;
    var selectedText = TextBox.value.substring(startPos, endPos)
   }
  alert("You selected: " + selectedText);
}

If you only need to know if a user has selected anything , you can do: 如果只需要知道用户是否选择了任何东西 ,则可以执行以下操作:

var hasSelected = (TextBox.selectionStart != undefined)
<script type="text/javascript">
function getSelText() {
    var txt = '';
     if (window.getSelection) {
        txt = window.getSelection();
     } else if (document.getSelection)  {
        txt = document.getSelection();
    }
    else if (document.selection) {
        txt = document.selection.createRange().text;
    } else {
            return;
        }
        document.aform.selectedtext.value =  txt;
}
</script>
<input type="button" value="Get selection" onmousedown="getSelText()" /> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM