简体   繁体   English

如何获取光标或插入符号所在的div?

[英]How to get the div in which the cursor or caret is placed?

I have three contenteditable divs and I want to know ,in which div the user has placed the cursor or caret through tab key or mouse click, 我有三个contenteditable div,我想知道,用户通过Tab键或鼠标单击将光标或插入符号放在了哪个div中,

    <div contenteditable="true"></div>
    <div contenteditable="true"></div>
    <div contenteditable="true"></div>

Can somebody help me out? 有人可以帮我吗?

I worked out a better solution ,this will need enclosing all the div tags that you want to check for the cursor into a parent div tag and using the document.getSelection() object we can get the id of the div in which the cursor or caret is placed 我制定了一个更好的解决方案,这需要将要检查光标的所有div标签封装到父div标签中,并使用document.getSelection()对象获取光标所在的div的ID。插入符放置

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset=utf-8 />
    <title>JS Bin</title>
    </head>
    <body>
      <div  id=parent>
            <div contenteditable="true" id=1>dddd
            </div>
            <div contenteditable="true" id=2>dddd
            </div>
            <div contenteditable="true" id=3>dddd
            </div>
      </div>
      <input type="label" id="showRes"></input>
    </body>
    <script type="text/javascript">
var div = document.getElementsByTagName('div');

var out=document.getElementById('parent');

function findCursor(){

var x=document.getSelection();
var r=x.getRangeAt(0);
var tempnode=document.createElement("div");
tempnode.setAttribute("id","cursorhook");
r.surroundContents(tempnode);
document.getElementById('showRes').value=tempnode.parentNode.id;
document.getElementById(tempnode.parentNode.id).removeChild(document.getElementById("cursorhook"));


}


out.onkeyup=findCursor;
out.onclick=findCursor;


    </script>
    </html>

Use document.activeElement . 使用document.activeElement This simply returns whichever element currently has focus on the page. 这仅返回当前在页面上具有焦点的任何元素。 ( MDN ) MDN

Use onfocus event which triggers whenever you are focussing an element ... 使用onfocus事件,该事件会在您聚焦某个元素时触发...

Example: 例:

var div = document.getElementsByTagName('div');

for(var i=0;i<div.length;i++)
   div[i].addEventListener('focus',function(){document.getElementById('showRes').value = this.id;},false);

HTML : HTML:

<div contenteditable="true" id=1></div>
<div contenteditable="true" id=2></div>
<div contenteditable="true" id=3></div>
<input type="label" id="showRes"></input>

Demo Fiddle 演示小提琴

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

相关问题 JavaScript-在contenteditable div中获取插入符号(光标)之前的CSS字母样式 - JavaScript - Get CSS style of letter which is before caret (cursor) in contenteditable div 如何获得包含图像的contenteditable div的插入位置 - how to get the caret position of a contenteditable div which contains images 光标(尖号)在contenteditable div中不可见 - cursor (caret) is not visible in contenteditable div 如何在特定的contenteditable div上设置插入符/光标位置? - How do I set the caret/cursor position on a specific contenteditable div? 如何在 contenteditable 元素(div)中设置插入符号(光标)position? - How to set the caret (cursor) position in a contenteditable element (div)? 如何在只读div中显示闪烁的文本光标/插入符号 - How to show blinking text cursor/caret in a read-only div 如何在单击按钮时删除 cursor 放置在输入框中的字符 - how to remove the character at which cursor is placed in an input box on button click 如何在插入符位置获取div ID - How to Get div id at caret position 获取插入符所在的单词? - Get the word upon which the caret sits within a contenteditable div? 获取多个可编辑的div中的最新插入符号位置,并将图像附加到最后出现光标的div中 - Get the latest caret position among multiple contenteditable div and append image to the div where the cursor was present lastly
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM