Javascript 从javascript制作超链接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8005694/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 04:22:08  来源:igfitidea点击:

make hyperlink from javascript

javascripthtml

提问by Raman

I want to use a hyperlink string in HTML page which I want to declare source link (URL) in my js file. Please tell me how can I call that URL from my js into html.

我想在 HTML 页面中使用超链接字符串,我想在我的 js 文件中声明源链接(URL)。请告诉我如何将我的 js 中的 URL 调用为 html。

Thanks

谢谢

回答by Michael Aaron Safyan

There are a number of different ways to do this. You could use document.createElement()to create a link element, and then inject the element into the DOM. Or you could use the .innerHTMLproperty of an element that you already have on the page to insert the link using text. Or you could modify the "href" attribute of an existing link on the page. All of these are possibilities. Here is one example:

有许多不同的方法可以做到这一点。您可以使用document.createElement()创建一个链接元素,然后将该元素注入到 DOM 中。或者您可以使用.innerHTML页面上已有的元素的属性来插入使用文本的链接。或者您可以修改页面上现有链接的“href”属性。所有这些都是可能的。这是一个例子:

Creating/Inserting DOM Nodes with DOM Methods

使用 DOM 方法创建/插入 DOM 节点

var link = document.createElement('a');
link.textContent = 'Link Title';
link.href = 'http://your.domain.tld/some/path';
document.getElementById('where_to_insert').appendChild(link);

Assuming you have something like this in your HTML:

假设您的 HTML 中有这样的内容:

 <span id="where_to_insert"></span>

Creating/Inserting DOM Content with innerHTML

使用 innerHTML 创建/插入 DOM 内容

And another example using innerHTML(which you should generally avoid using for security reasons, but which is valid to use in cases where you completely control the HTML being injected):

另一个使用示例innerHTML(出于安全原因,您通常应该避免使用它,但在您完全控制被注入的 HTML 的情况下使用它是有效的):

 var where = document.getElementById('where_to_insert');
 where.innerHTML = '<a href="http://your.domain.tld">Link Title</a>';

Updating the Attributes of a Named DOM Node

更新命名 DOM 节点的属性

Lastly, there is the method that merely updates the href attribute of an existing link:

最后,有一种方法只更新现有链接的 href 属性:

 document.getElementById('link_to_update').href = 'http://your.domain.tld/path';

... this assumes you have something like the following in your HTML:

...这假设您的 HTML 中有如下内容:

 <a id="link_to_update" href="javascript:void(0)">Link Title</a>

回答by James.Xu

Try this:

尝试这个:

var alink = document.createElement("a");
alink.href = "http://www.google.com";
alink.text = "Test Link";
document.getElementsByTagName("body")[0].appendChild(alink)

回答by xyz

From whatever I understand, You want to update hrefwith JS variable. You can use Jquery to achieve it. try $("a").attr("href", js_variable)Refer this for more details How to change the href for a hyperlink using jQuery

据我了解,您想href使用 JS 变量进行更新。您可以使用 Jquery 来实现它。尝试$("a").attr("href", js_variable)参考此了解更多详细信息 如何使用 jQuery 更改超链接的 href

回答by Sreekumar P

It seems like you would be able to do something like this:

看起来你可以做这样的事情:

Using Javascript.

使用 JavaScript。

var col2= document.getElementById('id_Of_Control');
col2.innerHTML="<a href='page2.html?" + params + "'>Page 2</a>";

where col2is another container control something like div,span, or any.

col2另一个容器控件在哪里div,例如span,或任何。

Using jQuery.

使用jQuery

Here I will recommend you to Use jQuery. So you can be more dynamic.

在这里我会推荐你​​使用 jQuery。所以你可以更有活力。

 $("#col2").append("<a href='page2.html?" + params + ">Page 2</a>");

OR

或者

$("#col2").after("<a href='page2.html?" + params + ">Page 2</a>");

OR

或者

$("#col2").before("<a href='page2.html?" + params + ">Page 2</a>");