Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/455.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从javascript创建超链接_Javascript_Html - Fatal编程技术网

从javascript创建超链接

从javascript创建超链接,javascript,html,Javascript,Html,我想在HTML页面中使用超链接字符串,我想在js文件中声明源链接(URL)。请告诉我如何将我的js中的URL称为html 谢谢试试这个: var alink = document.createElement("a"); alink.href = "http://www.google.com"; alink.text = "Test Link"; document.getElementsByTagName("body")[0].appendChild(alink) 据我所知,您希望使用JS变量更

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

谢谢

试试这个:

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

据我所知,您希望使用JS变量更新
href
。 您可以使用Jquery来实现它。 试试
$(“a”).attr(“href”,js\u变量)
有关更多详细信息,请参阅此
有很多不同的方法可以做到这一点。您可以使用创建一个link元素,然后将该元素注入DOM。或者,您可以使用页面上已有的元素的
.innerHTML
属性,使用文本插入链接。或者您可以修改页面上现有链接的“href”属性。所有这些都是可能的。以下是一个例子:

使用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);
假设您的HTML中有类似的内容:

 <span id="where_to_insert"></span>
 <a id="link_to_update" href="javascript:void(0)">Link Title</a>
更新命名DOM节点的属性

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

 document.getElementById('link_to_update').href = 'http://your.domain.tld/path';
。。。这假设您的HTML中有如下内容:

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

您似乎可以这样做:

使用Javascript。

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

$(“#col2”)。在(“”)之后;

(“”)之前的“$”;
谢谢大家,但问题是我在js文件中声明了URL,在html页面中声明了文本链接,同时在html中单击链接到js文件URL的链接。
$("#col2").after("<a href='page2.html?" + params + ">Page 2</a>");
$("#col2").before("<a href='page2.html?" + params + ">Page 2</a>");