태그 보관물: hyperlink

hyperlink

자바 스크립트를 사용하여 링크를 생성하려면 어떻게합니까? 모르겠습니다. 도움을 주시면 감사하겠습니다. EDIT1 : 질문에

제목 문자열과 링크 문자열이 있습니다. Javascript를 사용하여 페이지에 링크를 만들기 위해 두 가지를 결합하는 방법을 모르겠습니다. 도움을 주시면 감사하겠습니다.

EDIT1 : 질문에 더 많은 세부 사항 추가. 내가 이것을 알아 내려는 이유는 RSS 피드가 있고 제목과 URL 목록이 있기 때문입니다. 페이지를 유용하게 만들기 위해 제목을 URL에 연결하고 싶습니다.

EDIT2 : 나는 jQuery를 사용하고 있지만 완전히 처음이며이 상황에서 도움이 될 수 있다는 것을 알지 못했습니다.



답변

<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>


답변

JavaScript로

  1. var a = document.createElement('a');
    a.setAttribute('href',desiredLink);
    a.innerHTML = desiredText;
    // apend the anchor to the body
    // of course you can append it almost to any other dom element
    document.getElementsByTagName('body')[0].appendChild(a);
    
  2. document.getElementsByTagName('body')[0].innerHTML += '<a href="'+desiredLink+'">'+desiredText+'</a>';

    또는 @travis가 제안한 대로 :

    document.getElementsByTagName('body')[0].innerHTML += desiredText.link(desiredLink);
  3. <script type="text/javascript">
    //note that this case can be used only inside the "body" element
    document.write('<a href="'+desiredLink+'">'+desiredText+'</a>');
    </script>
    

JQuery 사용

  1. $('<a href="'+desiredLink+'">'+desiredText+'</a>').appendTo($('body'));
  2. $('body').append($('<a href="'+desiredLink+'">'+desiredText+'</a>'));
  3. var a = $('<a />');
    a.attr('href',desiredLink);
    a.text(desiredText);
    $('body').append(a);
    

위의 모든 예에서 앵커를 ‘본문’뿐만 아니라 모든 요소에 추가 할 수 desiredLink있으며, 앵커 요소가 가리키는 주소 desiredText를 보유하는 변수이며 표시 될 텍스트를 보유하는 변수입니다. 앵커 요소.


답변

JavaScript를 사용하여 링크 만들기 :

<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>


답변

몇 가지 방법이 있습니다.

JQuery와 같은 도우미없이 원시 Javascript를 사용하려면 다음과 같이 할 수 있습니다.

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>");


답변

    <script>
      _$ = document.querySelector  .bind(document) ;

        var AppendLinkHere = _$("body") // <- put in here some CSS selector that'll be more to your needs
        var a   =  document.createElement( 'a' )
        a.text  = "Download example"
        a.href  = "//bit\.do/DeezerDL"

        AppendLinkHere.appendChild( a )


     // a.title = 'Well well ... 
        a.setAttribute( 'title',
                         'Well well that\'s a link'
                      );
    </script>

  1. ‘Anchor Object’에는 링크, 텍스트를 설정하기위한 고유 한 * (상 속됨) * 속성이 있습니다. 그러니 그냥 사용하세요. .setAttribute 가 더 일반적이지만 일반적으로 필요하지 않습니다. a.title ="Blah"똑같이 할 것이고 더 명확합니다! .setAttribute 가 필요한 상황 은 다음과 같습니다.var myAttrib = "title"; a.setAttribute( myAttrib , "Blah")

  2. 프로토콜을 열어 둡니다. http : //example.com/path 대신 //example.com/path를 사용하는 것이 좋습니다. http :https :에서 example.com에 액세스 할 수 있는지 확인 하지만 사이트의 95 %가 둘 다에서 작동합니다.

  3. OffTopic : JS에서 링크를 만드는 것과는 관련이 없지만 알아두면 좋을 수도 있습니다.
    가끔은 크롬 dev-console에서와 같이A$("body")대신사용할 수있는 것처럼 처음 사용할 때 잘못된 호출 오류로귀하의 노력을 ‘경의’합니다. 할당이 .querySelector ( 클래스 메서드에대한 참조)를’잡기’ 때문 입니다. 으로당신은 또한 컨텍스트를 포함하는 것이다 (여기입니다) 당신은 얻을 개체 당신이 그것을 예상대로 작동 것이다 방법을.document.querySelector("body")_$ = document.querySelector.bind(...document


답변

원시 JavaScript를 사용하여 동적으로 하이퍼 링크를 만듭니다.

   var anchorElem = document.createElement('a');
   anchorElem.setAttribute("href", yourLink);
   anchorElem.innerHTML = yourLinkText;

   document.body.appendChild(anchorElem); // append your new link to the body


답변

이것을 안에 붙여 넣습니다.

<A HREF = "index.html">Click here</A>