如何使用javascript创建链接?

Xav*_*ier 117 html javascript jquery dom hyperlink

我有一个标题字符串和一个链接字符串.我不知道如何将两者结合在一起使用Javascript在页面上创建链接.任何帮助表示赞赏.

编辑1:为问题添加更多细节.我试图解决这个问题的原因是因为我有一个RSS提要并且有一个标题和URL列表.我想将标题链接到URL以使页面有用.

编辑2:我正在使用jQuery,但它是全新的,并不知道它可以帮助在这种情况下.

Jar*_*ish 216

<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>
Run Code Online (Sandbox Code Playgroud)

  • @Nadu-请停止编辑我的答案。如果您想说一件事,请添加自己的一句话;如果没有足够的“差异”来保证它,就没有足够的差异来保证编辑。 (3认同)

gio*_*_13 57

使用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);
    
    Run Code Online (Sandbox Code Playgroud)
  2. document.getElementsByTagName('body')[0].innerHTML += '<a href="'+desiredLink+'">'+desiredText+'</a>';
    
    Run Code Online (Sandbox Code Playgroud)

    或者,正如@travis所建议的那样:

    document.getElementsByTagName('body')[0].innerHTML += desiredText.link(desiredLink);
    
    Run Code Online (Sandbox Code Playgroud)
  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>
    
    Run Code Online (Sandbox Code Playgroud)

使用JQuery

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

在上面的所有示例中,您可以将锚点附加到任何元素,而不仅仅是"body",并且desiredLink是一个包含锚元素指向的地址desiredText的变量,并且是一个包含将在其中显示的文本的变量.锚元素.

  • 我认为你唯一遗漏的是:`document.getElementsByTagName('body')[0] .innerHTML + = desiredText.link(desiredLink);` (3认同)

Nav*_*eed 15

使用JavaScript创建链接:

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

要么

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

要么

<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>
Run Code Online (Sandbox Code Playgroud)


小智 11

有两种方法:

如果你想使用原始的Javascript(没有像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);
Run Code Online (Sandbox Code Playgroud)

另一种方法是将链接直接写入文档:

document.write("<a href='" + link + "'>" + text + "</a>");
Run Code Online (Sandbox Code Playgroud)


jmo*_*253 5

使用原始 JavaScript 动态创建超链接:

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

   document.body.appendChild(anchorElem); // append your new link to the body
Run Code Online (Sandbox Code Playgroud)


Nad*_*adu 5

    <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>
Run Code Online (Sandbox Code Playgroud)

  1. “锚对象”有自己的*(继承的)*属性,用于设置链接及其文本。所以就用它们吧。.setAttribute更通用,但通常不需要它。a.title ="Blah"也会做同样的事情并且更加清晰!需要.setAttribute的情况是这样的:var myAttrib = "title"; a.setAttribute( myAttrib , "Blah")

  2. 让协议保持开放状态。考虑只使用 //example.com/path,不是http://example.com/path。检查 example.com 是否可以通过http:https:访问,但 95% 的网站都可以在这两种方式上运行。

  3. OffTopic:这与在 JS 中创建链接并不真正相关,但也许很高兴知道: 有时就像在 chrome 开发控制台中,您可以使用$("body")代替document.querySelector("body")A ,第一次使用它时会出现非法调用_$ = document.querySelector错误,从而“尊重”您的努力。这是因为赋值只是“抓取” .querySelector (对方法的引用 )。您还将涉及上下文(这里是),并且您将获得一个可以按照您的预期工作的对象方法。.bind(...document