当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - Create a DOM DocumentFragment from Range with CSS Styles Applied

问题描述:

Suppose a document like this:

<style type="text/css">

.my-bold {

font-weight: bold;

}

</style>

<p id="first">Paragraph with <span class="my-bold">bold text</span></p>

<p id="second">Another Paragraph with <span class="my-bold">bold text</span></p>

I'd like to extract the second paragraph with styles applied:

<p id="second">Another Paragraph with <span class="my-bold" style="font-weight: bold">bold text</span></p>

Is there an API for this? I tried Range.cloneContents()

var p = document.getElementById( "second")

var range = document.createRange()

range.selectNode( p)

var html = range.cloneContents()

But this gives me only the HTML without styles:

<p id="second">Another Paragraph with <span class="my-bold">bold text</span></p>

A WebKit-only solution would be sufficient.

分享给朋友:
您可能感兴趣的文章:
随机阅读: