자바 펼쳐를 사용하여 링크를 만들려면 어떻게 사용합니까?
제목에 대한 링크와 링크에 대한 링크가 있습니다. 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로
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);
document.getElementsByTagName('body')[0].innerHTML += '<a href="'+desiredLink+'">'+desiredText+'</a>';
document.getElementsByTagName('body')[0].innerHTML += desiredText.link(desiredLink);
<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로
$('<a href="'+desiredLink+'">'+desiredText+'</a>').appendTo($('body'));
$('body').append($('<a href="'+desiredLink+'">'+desiredText+'</a>'));
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와 같은 도우미없이) 다음과 같이 할 수 있습니다.
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>
'Anchor Object'에는 링크, 텍스트를 설정하기위한 자체 * (상 속됨) * 속성이 있습니다. 그러니 그냥 사용하세요. .setAttribute 가 더 일반적이지만 일반적으로 필요하지 않습니다.
a.title ="Blah"
똑같이 할 것이고 더 명확합니다! .setAttribute 가 필요한 상황 은 다음과 같습니다.var myAttrib = "title"; a.setAttribute( myAttrib , "Blah")
프로토콜을 열어 둡니다. http : //example.com/path 대신 //example.com/path를 사용하는 것이 좋습니다. http : 및 https :에서 example.com에 액세스 할 수 있는지 확인 하지만 사이트의 95 %가 둘 다에서 작동합니다.
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>
참고 URL : https://stackoverflow.com/questions/4772774/how-do-i-create-a-link-using-javascript
'IT' 카테고리의 다른 글
C ++-std :: shared_ptr 또는 boost :: shared_ptr에 참조 전달 (0) | 2020.07.25 |
---|---|
Android : AsyncTask의 onPreExecute ()에 전달 변수를 전달하는 방법은 무엇입니까? (0) | 2020.07.25 |
DateTime에서 어떻게 딥 카피를 받습니까? (0) | 2020.07.25 |
각도 파일 업로드 (0) | 2020.07.25 |
Android- 가로 모드의 에뮬레이터, 화면이 회전하지 (0) | 2020.07.24 |