Sam*_*fes 6 html javascript dom
我试图用javascript找出特定节点的选择偏移量.
说我有以下HTML
<p>Hi there. This <strong>is blowing my mind</strong> with difficulty.</p>
Run Code Online (Sandbox Code Playgroud)
如果我从选择吹至困难,它给了我从偏移#text
节点的内部<strong>
.我需要从<p>
's innerHTML
和选择的长度偏移字符串.在这种情况下,偏移量为26,长度为40.
我的第一个想法是用字符串偏移等做一些事情,但你可以很容易地得到类似的东西
<p> Hi there. This <strong>is awesome</strong>. For real. It <strong>is awesome</strong>.</p>
Run Code Online (Sandbox Code Playgroud)
由于存在相同的节点,这会破坏该方法.我还需要选择丢弃节点.说我有这样的事情
<p>Hi there. <a href="#" rel="inserted">This <strong>is blowing</a> my mind</strong> with difficulty.</p>
Run Code Online (Sandbox Code Playgroud)
我想rel="inserted"
在计算时抛出一个元素.我仍然希望26和40作为结果.
解决方案需要递归.如果有一个<span>
与<strong>
它,它仍然需要遍历到<p>
.
解决方案需要删除任何元素的长度rel="inserted"
.内容很重要,但标签本身不重要.所有其他标签都很重要.当我做所有这些时,我强烈不希望从DOM中删除任何元素.
我document.getSelection()
用来获取选择对象.此解决方案只需在WebKit中工作.jQuery是一个选项,但如果可能的话,我更愿意没有它.
任何想法将不胜感激.
我无法控制HTML,我正在做所有这些.
小智 -3
也许你可以使用 jQuery 选择器来忽略 rel="inserted" ?
$('a[rel!=inserted]').doSomething();
Run Code Online (Sandbox Code Playgroud)
http://api.jquery.com/attribute-not-equal-selector/
您现在使用什么代码来选择从打击到困难?