반응형

- 선택 텍스트에 링크 생성

<script type="text/javascript">
function AddLink()
{
    //identify selected text
    var sText = document.selection.createRange();
    if (sText.text != "")
    {
      //create link
      document.execCommand("CreateLink");
      //change the color to indicate success
      if (sText.parentElement().tagName == "A")
      {
        sText.execCommand("ForeColor",false,"#FF0033");
      }
    }
    else
    {
        alert("Please select some text!");
    }  
}
</script>
<p unselectable="on">Select any portion of the following blue text, such as &quot;My
favorite Web site&quot;. Click the button to turn the selected text into a link. </p>
<p style="color= #3366CC">My favorite Web site is worth clicking on. Don't forget
to check out my favorite music group!</p>
<button onclick="AddLink()" unselectable="on">Click to add link</button>



- url 링크 생성

<HTML>
<BODY>
<H4 unselectable="on">연결을 생성하고 주소를 반환하는 예제</H4>
<SCRIPT>
function AddLink(){ // 선택된 텍스트를 인식한다.
  var sText=document.selection.createRange();
  if (!sText==''){  // 연결을 생성한다.
    document.execCommand('CreateLink');  // 문자열을 주소(URL)로 대체한다.
    if (sText.parentElement().tagName=='A'){
      sText.parentElement().innerText=sText.parentElement().href;
      document.execCommand('ForeColor','false','#FF0033');
    }
  } else alert('선택 가능한 적색 박스 속의 텍스트를 선택하라.');
}
</SCRIPT>
<P unselectable="on">적색 박스 속의 텍스트를 선택하고, 클릭하여 연결을 만들어 연결을 입력해 보라.</P>
<P style="color=#3366CC;border:solid 1 red">내 홈페이지, 내가 가장 많이 방문하는 페이지.</P>
<BUTTON onclick="AddLink()" unselectable="on">연결을 추가한다.</BUTTON>
</BODY>
</HTML>

반응형
LIST

+ Recent posts