简体   繁体   English

如何让window.getselection用于输入type = text字段

[英]How do I get window.getselection to work for an input type=text field

I have a contenteditable div, like so: 我有一个满足感的div,就像这样:

<div id="test" contentEditable="true" style="width: 600px; height:300px;">Lorem ipsum dolor sit amet</div>

for which I use the following code: 我使用以下代码:

<input type="button" value="Click me" onclick="alert(window.getSelection().focusOffset.toString());"></button>

Clicking on the button when I move the caret around in the div, returns to me the actual position (offset) of the caret within the div. 当我在div中移动插入符号时单击按钮,返回div中插入符号的实际位置(偏移量)。

The problem is when I replace the contenteditable div with an input type=text or password control, and keep the contenteditable property=true, and click on the button, I always get a zero. 问题是当我用输入类型=文本或密码控件替换contenteditable div,并保持contenteditable属性= true,然后单击按钮,我总是得到零。 Why is this? 为什么是这样?

Thanks for looking. 谢谢你的期待。

In most browsers, window.getSelection() only works with selections within text nodes and elements within the document. 在大多数浏览器中, window.getSelection()仅适用于文本节点内的选择和文档中的元素。 It doesn't apply to text within <input> and <textarea> elements (although in WebKit window.getSelection().toString() will return the selected text within a focussed text input or textarea. See http://jsfiddle.net/PUdaS/ ). 它不适用于<input><textarea>元素中的<textarea> (尽管在WebKit中window.getSelection().toString()将在焦点文本输入或textarea中返回所选文本。请参阅http://jsfiddle.net / PUdaS / )。 To get the selection within an input, use the input's selectionStart and selectionEnd properties: 要在输入中获取选择,请使用input的selectionStartselectionEnd属性:

<input type="text" id="test" value="Some text">

<input type="button" value="Click me"
    onclick="alert(document.getElementById('test').selectionEnd);">

Note that IE up to and including version 8 does not support the selectionStart and selectionEnd properties, and a different, more complicated solution is required. 请注意,直到并包括版本8的IE不支持selectionStartselectionEnd属性,并且需要一个不同的,更复杂的解决方案 IE doesn't support window.getSelection() either, so this code will work in all the browsers your original code does. IE也不支持window.getSelection() ,因此这段代码适用于原始代码所有的浏览器。

This will largely depend on the browser you are using, as this is a quirk of browser design and not JavaScript. 这在很大程度上取决于您使用的浏览器,因为这是浏览器设计的一个怪癖,而不是JavaScript。

What happens in your case is when you click an <input type="button"> while having a <div> highlighted, the <div> remains highlighted. 在您的情况下发生的情况是,当您突出显示<div>时单击<input type="button"><div>仍然突出显示。 However, when you click an <input type="button"> while having an <input type="text"> or <textarea> highlighted, they lose focus and the highlight is removed (thus making your code fail). 但是,当您突出显示<input type="text"><textarea>时单击<input type="button">时,它们将失去焦点并删除突出显示(从而使代码失败)。

I would take a closer look at what you are trying to accomplish and consider rethinking how you are approaching this problem. 我会仔细研究一下你想要完成什么,并考虑重新思考你是如何解决这个问题的。

2 remarks: 2评论:

<input... > tag is not a tag and shall be closed accordingly like <input ... /> otherwise you should use the <button> tag (but not within a for X-browser compliance reasons) <input... >标签不是标签,应该像<input ... />一样关闭,否则你应该使用<button>标签(但不能用于X浏览器合规性原因)

If you don't want to enter into a non-end story, you may reconsider the way to handle your point as follow : keep your <div> as it is and fireup a DOM event on that (instead of using the onclick event on the tag). 如果你不想进入一个非终结的故事,你可能会重新考虑处理你的观点的方式如下:保持你的<div>不变并在其上激活一个DOM事件(而不是使用onclick事件)标签)。 If you can read french, I have summarized plenty of examples here . 如果你能读法语,我在这里总结了很多例子。 This way is significant heavier as your first way, but much more powerful afterwards as you can fire up events how you want on the DOM, use callback functions and so on.. 这种方式作为你的第一种方式是重要的,但之后更强大,因为你可以在DOM上启动事件你想要的方式,使用回调函数等等。

The principle is the following : 原则如下:
HTML page on one hand 一方面是HTML页面

<div id="test"...>Lorem..</div>  
<script src="..."/>  

Non intrusive javascript code on the other hand 另一方面,非侵入式javascript代码

// your event handler here
function Dothisorthat(e) {
 // pop up the dom content or edit it or start an ajax request or whatever you need here
 return false;
}

// This is read in the third place  
function attacherAction() {
// Attach the action "Dothisorthat" on the event "onclick" occuring on your div "test"
id ='test'; // your div id
if (document.getElementById(id)) {
       zLink = document.getElementById(id);
       zLink.onclick = function() {
           Dothisorthat(this); // in your case, you can write a small function to pop up or edit the div content
       }
   }
}  
// This is read second
function addLoadEvent(func) {  
  var oldonload = window.onload;  
  if (typeof window.onload != 'function') {  
    window.onload = func;  
  } else {  
    window.onload = function() {  
      if (oldonload) {  
        oldonload();  
      }  
      func();
    }
  }
}  
    // This is read first
addLoadEvent(attacherAction);  

Hope that helps 希望有所帮助

暂无
暂无

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

相关问题 如何使用html标记从window.getSelection()。getRangeAt(0)中包装文本选择? - How do I wrap a text selection from window.getSelection().getRangeAt(0) with an html tag? 使用window.getSelection,我怎么知道会影响来自两个不同节点的文本? - With window.getSelection, how do I know affect text from two different nodes? 如何使用“window.getSelection()”获取HTML - how to get HTML with “window.getSelection()” 当文本由换行时,window.getSelection()。getRange(0)不起作用<mark> - window.getSelection().getRange(0) does not work when text is wrapped by <mark> 我如何为window.getSelection值分配文本方向RTL,我试过但是如何从文本区域的现有值中删除getSelection - how to i assign text direction RTL for window.getSelection value, I tried but how to remove getSelection from existing value of text area window.getSelection() 给了我选定的文本,但我想要 HTML - window.getSelection() gives me the selected text, but I want the HTML 如何通过Range对象和window.getSelection()获取与body相关的Character偏移量? - how can I get the Character offset related to body with Range Object and window.getSelection()? window.getSelection()如何在contenteditable javascript中获取当前节点 - window.getSelection() how to get the current node inside contenteditable javascript Android:window.getSelection()在Webview中不起作用 - Android: window.getSelection() does not work in webview 获取 window.getSelection().anchorNode 的属性 - Get Attributes of window.getSelection().anchorNode
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM