简体   繁体   English

从contenteditable中的插入符位置返回最接近的跨度

[英]Return closest span from caret position in contenteditable

If caret placed on word gallery ( or on other word between those two span tags ) how can I get data-id from span element with jQuery? 如果将插入号放在单词 (或这两个span标签之间的其他单词)上,如何使用jQuery从span元素获取数据ID?

<div class="editor" contenteditable="true">
<p>Lorem Ipsum is simply dummy text of the printing and
typesetting industry. Lorem Ipsum has been the industry's standard dummy text
ever since the 1500s, when an unknown printer took a <span data-id="1"></span>galley
of type and scrambled it to make a type specimen book.</p>
<p> It has survived not only five centuries, but also the leap into electronic
typesetting, remaining essentially unchanged.<span data-id-end="1"></span> It was
popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum
passages, and more recently with desktop publishing software like Aldus PageMaker including
versions of Lorem Ipsum.</p>

You can get attribute data-id using this code in jQuery 您可以在jQuery中使用此代码获取属性data-id

 console.log($("span").attr("data-id")); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <span data-id="3"></span> 

You can get idea from it 你可以从中得到想法

 var texta=$("span").nextUntil("span").text().trim(); var starting_sign = texta.indexOf("+"); var ending_sign = texta.indexOf("."); console.log(texta.substring(starting_sign, ending_sign)); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <span></span> This is text + pick it . for example <span></span> 

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

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