如何添加一个var到href [英] How to add a var to href
本文介绍了如何添加一个var到href的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要这样的东西:
var link = " http://www.google.com";
<a href='link' />
所以我需要使用一个变量为 href
链接链接。这是可能吗?
So I need to use a variable as href
attribue of a link. Is this possible?
它与< a href ='+ link +'/>
。有更好的方法吗?
It worked with "<a href= '" + link + "' />"
. Is there a better way?
推荐答案
您应该在服务器上设置href值,这是不好的设计,为此使用脚本
You should set the href value on the server, it is bad design to use scripting for this.
但是,为了回答这个问题,可以通过使用脚本来设置A元素在DOM中的href属性,例如: p>
However, to answer the question, it can be done by using script to set the href property of the A element once it is in the DOM, e.g.:
<a href="<a useful URI if javascript not available>" id="a0">foo</a>
<script type="text/javascript">
var a = document.getElementById('a0');
if (a) {
a.href = "http://www.google.com";
}
</script>
当然还有其他一千种方法,都有他们的陷阱。所以设置服务器上的值并避免它们。
Of course there are a thousand other ways, all with their pitfalls. So set the value on the server and avoid them all.
--
Rob
这篇关于如何添加一个var到href的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文