javascript - Highlight including different nodes -
edit: found solution in post remove highlight added selected text using javascript?
i´m making script highlight texts. i´m using <span> tags. part easy come across problem. when highlighting includes different nodes need close <span> tag before parent close, , open 1 time again de <span> in new node. not explanation set example:
<body> <p id="0">lorem ipsum</p> <div id="1">dolor sit down amet</div> </body> i select highlighting :
ipsum</p><div id="1">dolor sit down then have close <span> before </p> , open after <div> programatically. ideas how can it? prefer script pure javascript, help appreciated.
here function highlight:
function surroundselection(element) { if (window.getselection) { var sel = window.getselection(); if (sel.rangecount) { console.log (sel); var range = sel.getrangeat(0).clonerange(); range.surroundcontents(element); sel.removeallranges(); sel.addrange(range); } } } where element tag ( in case <span>)
here have, still problem of highlight different nodes http://jsfiddle.net/nacles/4l6d57bs/
you have start different spam on every part of code you, in example:
<span>ipsum</span></p><div id="1"><span>dolor sit</span> so you, set span in begining , end of string or '<' if tag opening
note there nested tag block, if tag close
set closing span.there lot of cases think not difficult.
javascript html
No comments:
Post a Comment