在 <href> 标签内使用 Javascript 变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23883924/
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
using Javascript variable inside a <href> tag
提问by user3678812
Consider I have a Javascript variable named "link" which contains a url like this: www.google.com. I need to include this variable in href tag at 2 places, like :
考虑我有一个名为“link”的 Javascript 变量,它包含一个这样的网址:www.google.com。我需要在 2 个地方的 href 标签中包含这个变量,比如:
<a href="link">link</a>
this should return something like this:
这应该返回如下内容:
I tried various ways but failed. Is it possible to do like this ? The Javascript variables should be used at both places.
我尝试了各种方法但都失败了。有可能这样做吗?Javascript 变量应该在这两个地方使用。
Note:I need to use the variable inside <a>
tag also
注意:我还需要使用<a>
标签内的变量
thanks
谢谢
回答by T J
Assume you have the following in your html
假设您的 html 中有以下内容
<a href="link" class='dynamicLink'>link</a>
<a href="link" class='dynamicLink'>link</a>
You can do the following
您可以执行以下操作
var href = "http://www.google.com"; //any other link as wish
var links = document.getElementsByClassName('dynamicLink');
for (var i = 0; i < links.length; i++) {
links[i].href = href;
links[i].innerHTML = href.replace('http://',"");
}
回答by Radhakrishna Rayidi
You can use the following code.
您可以使用以下代码。
<a id="myLink" href="link">link</a>
in your javascript try
在你的 javascript 中尝试
<script>
var link = "http://www.google.com/";
document.getElementById('myLink').setAttribute("href",link);
document.getElementById('myLink').innerHTML = link;
// Here the link is your javascript variable that contains the url.
</script>
回答by Rui Lima
Sorry for any typo. Writing on my phone. Can't you create the anchor on the fly?
抱歉有任何错字。写在我的手机上。您不能即时创建锚点吗?
anchor =Document.createElement (...)
anchor.href=yourvar-value
anchor.innerText=your-value?
If a more complex thing is needed search for javascript databiding on Google
如果需要更复杂的事情,请在 Google 上搜索 javascript databiding
Take care
小心
回答by Nitish Dhar
You can do it like this -
你可以这样做 -
WORKING DEMO- http://codepen.io/nitishdhar/pen/rJLEH
工作演示- http://codepen.io/nitishdhar/pen/rJLEH
HTML Part
HTML 部分
<a href="" class="link">Link will be filled by javascript</a>
You need to place a specific class in the a tag. We will need this to get hold of this element from jQuery.
您需要在 a 标签中放置一个特定的类。我们将需要它来从 jQuery 中获取这个元素。
Javascript Part (Using jQuery)
Javascript 部分(使用 jQuery)
var link = "http://google.com";
$(document).ready(function(){
$('.link').attr('href', link);
});
We need to create a variable, link, which will hold the URL you want to assign to the a tag. Then on document ready, we get hold of the a element & update its href attribute with the link.
我们需要创建一个变量,link,它将保存您要分配给 a 标签的 URL。然后在文档准备好时,我们获取 a 元素并使用链接更新其 href 属性。
回答by Hamster
You can do this easy by jquery.
您可以通过 jquery 轻松完成此操作。
1st give your a class/id.
第一,给你一个班级/身。
<a class="mylink">link</a>
Then you can do something like:
然后你可以做这样的事情:
<script type='text/javascript'>
$(document).ready(function () {
$("a.mylink").attr("href", link);
});
</script>
If you want the text to change as well you will have to add
如果您希望文本也更改,则必须添加
$("a.mylink").text(link);
to $(document).ready
as well.
也$(document).ready
一样。
See it working: http://jsfiddle.net/symXp/
看到它工作:http: //jsfiddle.net/symXp/