English 中文(简体)
封顶门口的门顶
原标题:Getting character offset inside nested html tags

I ve got HTML Code similar to this one:

<pre>
    words words words
    words <span> words mystery words</span>
    words words words
</pre>

关于pre的标签,我想得到“神秘”的特性的抵消。 我可以拿到“span> tag”使用“Node”财产,但我无法找到一个途径来获得“<>pre标签”。

最佳回答

http://www.w3.org/TR/DOM-level-2-Traversal-Range/ranges.html 为此:

function getCharOffsetRelativeTo(container, node, offset) {
    var range = document.createRange();
    range.selectNodeContents(container);
    range.setEnd(node, offset);
    return range.toString().length;
}

例:

var sel = window.getSelection();
var pre = document.getElementById("your_pre_id");
var offset = getCharOffsetRelativeTo(pre, sel.anchorNode, sel.anchorOffset);

洞穴:

  • This will work in all major browsers except IE <= 8. If you need a solution for IE then I can provide it.
  • This function will count characters inside <script> or <style> tags and invisible elements (hidden by CSS display: none, for example).
问题回答

您可以写出一种方法,以补偿性办法计算它。 这种方法可确定被遗忘的特性,然后将母子的分数跨出,然后从小到一开始......再到所期望的标签(之前)找到,或者直到你从tag子中排出为止。





相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签