开发者

selecting HTML from a document using Javascript

开发者 https://www.devze.com 2023-03-18 06:04 出处:网络
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 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

精彩评论

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