【问题标题】:How do I get window.getselection to work for an input type=text field如何让 window.getselection 为输入类型 = 文本字段工作
【发布时间】:2011-07-01 14:59:58
【问题描述】:

我有一个内容可编辑的 div,如下所示:

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

我使用以下代码:

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

当我在 div 中移动插入符号时单击该按钮,返回给我插入符号在 div 中的实际位置(偏移量)。

问题是当我用输入类型=文本或密码控件替换 contenteditable div,并保持 contenteditable 属性=true,然后单击按钮时,我总是得到一个零。这是为什么呢?

感谢收看。

【问题讨论】:

    标签: javascript html contenteditable caret


    【解决方案1】:

    在大多数浏览器中,window.getSelection() 仅适用于文本节点内的选择和文档内的元素。它不适用于&lt;input&gt;&lt;textarea&gt; 元素中的文本(尽管在WebKit 中window.getSelection().toString() 将在焦点文本输入或文本区域中返回选定的文本。参见http://jsfiddle.net/PUdaS/)。要在输入中获取选择,请使用输入的 selectionStart and selectionEnd 属性:

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

    请注意,IE 版本 8(包括版本 8)不支持 selectionStartselectionEnd 属性,并且需要 different, more complicated solution。 IE 也不支持window.getSelection(),所以这段代码可以在你的原始代码支持的所有浏览器中运行。

    【讨论】:

    • 甚至在我打开这个问题之前,我就知道我会在这里找到你的答案:)
    • @Šime:似乎是我的专业科目:)
    【解决方案2】:

    这在很大程度上取决于您使用的浏览器,因为这是浏览器设计的一个怪癖,而不是 JavaScript。

    在您的情况下,当您在突出显示 &lt;div&gt; 的同时单击 &lt;input type="button"&gt; 时,&lt;div&gt; 仍然突出显示。但是,当您在突出显示 &lt;input type="text"&gt;&lt;textarea&gt; 的同时单击 &lt;input type="button"&gt; 时,它们会失去焦点并移除突出显示(从而使您的代码失败)。

    我会仔细研究您要完成的工作,并考虑重新考虑您是如何解决这个问题的。

    【讨论】:

    • 有趣。我只是使用输入 type=button 来说明一点。例如,我可以在 onkeyup 事件上使用警报,这仍然会给我零:(
    • 我不这么认为。我认为问题在于window.getSelection() 根本不适用于文本输入。
    • 另外,我注意到当我使用输入 type=text 加载页面时,我没有点击它(虽然它加载了里面的文本),我点击了按钮,我将索引设为 3。我不确定这是什么意思
    • 蒂姆,你知道我如何获得文本输入字段(文本、密码、文本区域)的插入符号位置吗?
    • @Freakishly:是的。我已经发布了答案。
    【解决方案3】:

    2 备注:

    &lt;input... &gt; 标签不是标签,应该像 &lt;input ... /&gt; 一样关闭,否则你应该使用 &lt;button&gt; 标签(但不是出于 X 浏览器合规性的原因)

    如果你不想进入一个没有结局的故事,你可以重新考虑处理你的观点的方法如下:保持你的 &lt;div&gt; 原样并启动一个 DOM 事件在那(而不是在标签上使用 onclick 事件)。如果你会读法语,我总结了很多例子here。这种方式比你的第一种方式要重得多,但之后更强大,因为你可以在 DOM 上按你想要的方式触发事件,使用回调函数等等..

    原理如下:
    一方面是 HTML 页面

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

    另一方面,非侵入式 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);  
    

    希望有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-05
      • 2016-11-10
      • 1970-01-01
      • 2017-02-13
      • 1970-01-01
      • 2023-03-10
      相关资源
      最近更新 更多