开发者

selecting HTML from a document using Javascript

I want to get a HTML selection out of the document.

Say, I h开发者_运维百科ave <u>Hi <i>I am <b>TOM</b></i></u> displayed.

I want to select am TO.

The following JS

var span = document.createElement( 'SPAN' );
span.appendChild( window.getSelection().getRangeAt(0).cloneContents() );
span.innerHTML;

only displays am <b>TO</b>. Rest of the tags which have influence on the selection are lost.

Is there any way to get these lost tags under selection too?

Thanks :)


You could simply append the opening tags before the window.getSelection and append the closing tags after it if this is a specific problem (as opposed to a general problem in which you cannot anticipate the tags used) .

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜