Converting Range or DocumentFragment to string

前端 未结 6 1143
无人共我
无人共我 2021-02-05 03:07

Is there a way to get the html string of a JavaScript Range Object in W3C compliant browsers?

For example, let us say the user selects the following: Hello

6条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-05 03:28

    To spell out an example from here:

    //Example setup of a fragment 
    var frag = document.createDocumentFragment(); //make your fragment 
    var p = document.createElement('p'); //create 

    test

    DOM node p.textContent = 'test'; frag.appendChild( p ); //Outputting the fragment content using a throwaway intermediary DOM element (div): var div = document.createElement('div'); div.appendChild( frag.cloneNode(true) ); console.log(div.innerHTML); //output should be '

    test

    '

提交回复
热议问题