2011-03-07 3 views

답변

17
다음은 모든 주요 브라우저에서이 작업을 수행하고 this answer의 정확한 중복됩니다

:

또한
function getSelectionHtml() { 
    var html = ""; 
    if (typeof window.getSelection != "undefined") { 
     var sel = window.getSelection(); 
     if (sel.rangeCount) { 
      var container = document.createElement("div"); 
      for (var i = 0, len = sel.rangeCount; i < len; ++i) { 
       container.appendChild(sel.getRangeAt(i).cloneContents()); 
      } 
      html = container.innerHTML; 
     } 
    } else if (typeof document.selection != "undefined") { 
     if (document.selection.type == "Text") { 
      html = document.selection.createRange().htmlText; 
     } 
    } 
    return html; 
} 
+0

환상적, 고맙습니다! – Zebra

+0

[팀의 답변과 동일] (http://stackoverflow.com/a/6668159/1269037)이 중복 된 질문입니다. –

+0

@DanDascalescu : 다른 질문에서 내 답변을 복제하기 때문에 이것을 downvoted 있습니까? –