2013-05-08 3 views
0

여기에서 하이퍼 링크를 사용하고 하이퍼 링크를 클릭 할 때 자바 스크립트를 사용하여 선택한 텍스트 (마우스 포인터 또는 강조 표시된 텍스트를 사용하여 선택한 텍스트)를 복사하려고합니다. 난 단지 작동하고 마우스 pointer.my 자바 스크립트에서 내가 선택한 텍스트 상자에 텍스트 나 하이라이트를 선택하려는하지만텍스트 상자에서 선택한 텍스트를 얻는 방법

<li><a href="#" onclick="JAVASCRIPT:return Edit();">Candidate Name</a> </li> 
<script type="text/javascript"> 
    function Edit() { 
     alert("hiii"); 
     document.getElementById('<%=txtbox.ClientID%>').value = document.getElementById('<%=divtext.ClientID%>').innerHTML; 
     return true; 
    } 
</script> 
<div> 
    <asp:TextBox ID="txtbox" runat="server"></asp:TextBox> 
</div> 
<div id="divtext" runat="server"> 
    TCS Infosys Wipro HP HCL Microsoft Facebook Facebook Facebook 
</div> 
+0

[이 항목 확인] (http://jsfiddle.net/dKaJ3/2/) http://stackoverflow.com/questions/4652734/return-html-from-a-user-selection/4652824#4652824 – Damith

+0

감사합니다. Damith ,하지만 내가 선택한 컨텍스트 메뉴에서 전체 페이지가 아닌 div 영역을 선택하면 도움이 될 수 있습니다. 나는 페이지의 다른 부분이 아니라 divtext에서 오른쪽 클릭을 원한다는 것을 의미합니다. – SANDEEP

답변

0


<!DOCTYPE html> 
<html> 
<script type="text/javascript"> 
function getSelectionText(divID) { 
    var selectedText = ""; 
    if (window.getSelection) { 
     var sel = window.getSelection(); 
     var div = document.getElementById(divID); 

     if (sel.rangeCount) { 
      // Get the selected range 
      var range = sel.getRangeAt(0); 

      // Check that the selection is wholly contained within the div text 
      if (range.commonAncestorContainer == div.firstChild) { 
       var selectedText = range.toString(); 
       } 
      } 
     } 
    return selectedText; 
    } 

    function Edit() { 
     var selectedText = getSelectionText("divtext") 
     document.getElementById("txtbox").value = selectedText; 
     return true; 
    } 
</script> 
<body> 
     <li><a href="#" onclick="JAVASCRIPT:return Edit();">Candidate Name</a> </li> 
     <div> 
      <input type="text" ID="txtbox" /> 
     </div> 
     <div id="divtext"> 
      TCS Infosys Wipro HP HCL Microsoft Facebook Facebook Facebook 
     </div> 
</body> 

을 시도 사업부의 전체 텍스트를 복사
+0

안녕하세요 Navnath, 고마워요. 하지만 전 선택한 컨텍스트 메뉴에서 전체 페이지가 아닌 div 영역을 선택하면 도움이됩니다. 나는 페이지의 다른 부분에 divtext를 오른쪽 클릭하고 싶다고 말하고 싶다. – SANDEEP

+0

@ 샌드위치 ... 나의 새로운 해결책을 확인하라. –

관련 문제