【问题标题】:How do I create a link using javascript?如何使用 javascript 创建链接?
【发布时间】:2011-06-13 22:52:35
【问题描述】:

我有一个标题字符串和一个链接字符串。我不确定如何将两者放在一起以使用 Javascript 在页面上创建链接。任何帮助表示赞赏。

EDIT1:为问题添加更多细节。 我试图弄清楚这一点的原因是因为我有一个 RSS 提要并有一个标题和 URL 列表。我想将标题链接到 URL 以使页面有用。

EDIT2:我正在使用 jQuery,但对它完全陌生,不知道它在这种情况下会有所帮助。

【问题讨论】:

  • 您是否使用 jQuery 或其他工具(Mootools、Dojo、Atlas 等)加载 RSS 提要?如果您尝试根据页面加载时获取的第三方 RSS 列表动态创建锚标记,我建议使用 jQuery 库或其他添加元素。这种情况下的细节对于了解需要做什么很重要。然而,DOM 方法是一个有用的例子。
  • 试试这个link我认为它可以是有益的

标签: javascript jquery html dom hyperlink


【解决方案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>

【讨论】:

  • 这是一个非常通用的使用 DOM 方法向页面添加锚标记的示例。例如,appendChild 方法可以是列表元素、TD 或页面中的其他元素。见:quirksmode.org
  • @Nadu - 请停止编辑我的答案。如果您想表达特定的内容,请添加您自己的内容;如果它没有足够“不同”来保证它,它也没有足够不同来保证编辑。
  • plnkr.co/edit/mV7nOBIHa6hMNaVIPG75?p=preview 我已经创建了一个 plunker 示例。
【解决方案2】:

使用 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 是一个变量保存将在锚元素中显示的文本。

【讨论】:

  • 我认为您遗漏的唯一一个是:document.getElementsByTagName('body')[0].innerHTML += desiredText.link(desiredLink);
  • 为了避免 XSS,在构建 HTML 时应避免字符串连接 (+) 和 .innerHTML。使用 jQuery,.attr("href", desiredLink).text(desiredText) 就是你想要的。
【解决方案3】:

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

【讨论】:

    【解决方案4】:

    有几种方法:

    如果您想使用原始 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);
    

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

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

    【讨论】:

    • 我绝对更喜欢第一个选项。为此 +1,但混合 JS 和 HTML 混合了内容和行为,它们应该是分开的。过度使用,可能会导致维护噩梦。
    • 我也倾向于第一个选项,但也许使用 JQuery 来达到相同的效果(为了可读性和易于维护)。
    • 你应该避免使用 document.write stackoverflow.com/questions/4520440/…
    【解决方案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>
    1. “锚对象”具有自己的*(继承)* 属性,用于设置链接,即其文本。所以只需使用它们。 .setAttribute 更通用,但您通常不需要它。 a.title ="Blah" 也会这样做,而且更清楚! 那么需要 .setAttribute 的情况是这样的:var myAttrib = "title"; a.setAttribute( myAttrib , "Blah")

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

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

    【讨论】:

      【解决方案6】:

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

         var anchorElem = document.createElement('a');
         anchorElem.setAttribute("href", yourLink);
         anchorElem.innerHTML = yourLinkText;
      
         document.body.appendChild(anchorElem); // append your new link to the body
      

      【讨论】:

      • 使用`anchorElem.text = yourLinkText; ` 而不是更清晰的 innerHTML。是的,考虑一下如果 yourLinkText 可能是“
      【解决方案7】:

      你把这个贴在里面:

      &lt;A HREF = "index.html"&gt;Click here&lt;/A&gt;

      【讨论】:

      • OP 明确要求使用 JavaScript(不是 HTML)创建链接!
      猜你喜欢
      • 2019-11-07
      • 2019-08-31
      • 1970-01-01
      • 2011-06-06
      • 2018-10-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多