2010-01-15 4 views
6

문서 선택 (현재 마우스 또는 키보드로 선택한 사용자)을 수정하고 크로스 브라우저 방식으로 수정하는 방법은 무엇입니까?자바 스크립트에서 문서 선택을 수정하는 방법?

+2

"문서 선택 수정"의 의미에 대해 자세히 설명 할 수 있습니까? – sberry

+0

현재 마우스 또는 키보드로 선택한 사용자 .... 분명합니까? 내 가난한 영어에 대한 미안. – lovespring

+0

http://stackoverflow.com/questions/2075304/how-to-modify-the-document-selection-in-javascript를 참조하십시오. iframe.window.selection을 사용하십시오. –

답변

6

본인은 실제 도움을 줄만큼 텍스트를 선택하지 않았지만 수행하려는 작업을 수행 할 수 있습니다. 다음과 같은 두 가지 기능으로보고 싶은 것 :

  1. createRange()MSDN을 |

  • getRangeAt()MDCMDC 나는 그것을 크로스 브라우저를 구현할 수 있습니다 알고 있습니다. 당신은 여기에 행동에 그것의 일부를 볼 수 있습니다

    http://fuelyourcoding.com/a-few-strategies-for-using-javascript/

    를 아래로 스크롤하고, 에버 노트 스크립트를 사용하는 코끼리 아이콘을 클릭. 그러나 내 스크립트는 먼저 주 컨텐츠 영역 (주황색으로 표시됨)을 선택하고 캡처가 완료되면 선택을 취소합니다.

    여기에 미니 jQuery 플러그인이 있습니다. 그것은 어떤 사이트에서 나에 의해 적응되었고, 의견이 말하는 것처럼, 나는 기억하지 못하는 것에 대해 끔찍한 느낌을 가진다. 참고하기 위해 정말 중요한 내가 jQuery를에 적응,하지만 그들은 그것을 수행하는 방법을 설명 곳이 코드는 어떤 사이트에서 온 :

    // Adapted this from somewhere. Feel horrible for not remembering. 
    $.fn.autoSelect = function(){ 
        var selectTarget = this[0]; // Select first element from jQuery collection 
        if(selectTarget != null) { 
         if(selectTarget.tagName == 'TEXTAREA' || (selectTarget.tagName == "INPUT" && selectTarget.type == "text")) { 
          selectTarget.select(); 
         } else if(window.getSelection) { // FF, Safari, Opera 
          var sel = window.getSelection(); 
          var range = document.createRange(); 
          range.selectNode(selectTarget); 
          sel.removeAllRanges(); 
          sel.addRange(range); 
         } else { // IE 
          document.selection.empty(); 
          var range = document.body.createTextRange(); 
          range.moveToElementText(selectTarget); 
          range.select(); 
         }; 
        }; 
        return this; // Don't break the chain 
    }; 
    

    이 스크립트가 온라인으로 몇 군데 보인다, 그러나 here is another variation on it

  • 5

    예를 들어 가장 쉬운 방법은 요소의 내용을 포함하도록 사용자 선택 항목을 이동하려는 경우를 가정 해 보겠습니다. 다음은 모든 주요 브라우저에서 작동합니다.

    function selectElementContents(el) { 
        var body = document.body, range, sel; 
        if (body.createTextRange) { 
         range = body.createTextRange(); 
         range.moveToElementText(el); 
         range.select(); 
        } else if (document.createRange && window.getSelection) { 
         range = document.createRange(); 
         range.selectNodeContents(el); 
         sel = window.getSelection(); 
         sel.removeAllRanges(); 
         sel.addRange(range); 
        } 
    } 
    
    selectElementContents(document.getElementById("someElement")); 
    
    +0

    고마워요, 팀. – lovespring

    관련 문제