복붙노트

[JQUERY] 어떻게 자바 스크립트를 사용하여 링크를 만들려면 어떻게해야합니까?

JQUERY

어떻게 자바 스크립트를 사용하여 링크를 만들려면 어떻게해야합니까?

해결법


  1. 1.

    <html>
      <head></head>
      <body>
        <script>
          var a = document.createElement('a');
          var linkText = document.createTextNode("my title text");
          a.appendChild(linkText);
          a.title = "my title text";
          a.href = "http://example.com";
          document.body.appendChild(a);
        </script>
      </body>
    </html>
    

  2. 2.위의 모든 예에서 당신은뿐만 아니라 '몸'으로, 모든 요소에 앵커를 추가 할 수 있으며, desiredLink은 당신의 앵커 요소의 포인트 및 desiredText이 될 것입니다 텍스트를 유지하는 변수가있는 주소를 보유하고 변수 앵커 요소에 표시.

    위의 모든 예에서 당신은뿐만 아니라 '몸'으로, 모든 요소에 앵커를 추가 할 수 있으며, desiredLink은 당신의 앵커 요소의 포인트 및 desiredText이 될 것입니다 텍스트를 유지하는 변수가있는 주소를 보유하고 변수 앵커 요소에 표시.


  3. 3.자바 스크립트를 사용하여 링크를 만듭니다 :

    자바 스크립트를 사용하여 링크를 만듭니다 :

    <script language="javascript">
    <!--
    document.write("<a href=\"www.example.com\">");
    document.write("Your Title");
    document.write("</a>");
    //-->
    </script>
    

    또는

    <script type="text/javascript">
    document.write('Your Title'.link('http://www.example.com'));
    </script>
    

    또는

    <script type="text/javascript">
    newlink = document.createElement('a');
    newlink.innerHTML = 'Google';
    newlink.setAttribute('title', 'Google');
    newlink.setAttribute('href', 'http://google.com');
    document.body.appendChild(newlink);
    </script>
    

  4. 4.몇 가지 방법이 있습니다 :

    몇 가지 방법이 있습니다 :

    당신이 (JQuery와 같은 도우미없이) 원시 자바 스크립트를 사용하려는 경우, 당신은 뭔가를 할 수 있습니다 :

    var link = "http://google.com";
    var element = document.createElement("a");
    element.setAttribute("href", link);
    element.innerHTML = "your text";
    
    // and append it to where you'd like it to go:
    document.body.appendChild(element);
    

    다른 방법은 문서에 링크를 직접 작성하는 것입니다 :

    document.write("<a href='" + link + "'>" + text + "</a>");
    

  5. 5.