如何仅使用javascript将变量的值传递给特定的页面元素? [英] How do I pass the value of a variable to an specific page element with solely javascript?

查看:61
本文介绍了如何仅使用javascript将变量的值传递给特定的页面元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有:

var url =http://www.google.html;

var url="http://www.google.html";

如何将url变量值作为页面中特定锚标记的href值?
我知道str.link()但是如何将它与特定元素一起使用?我可以使用链接*(构建图像的锚点吗?这对我来说似乎不起作用。感谢您的帮助

how do I put the url variable value as the href value of an specific anchor tag in the page? I know about str.link() but how I can use that with an specific element? and can I use link*( to build anchors of images? That didn't seem to work for me. Thanks for the help

推荐答案

对于锚元素:

<a id="myAnchor">link me!</a>

...

<script type="text/javascript">
var url = "http://www.google.html";  // .html is the new .com
var myAnchor = document.getElementById('myAnchor');
myAnchor.href = url;
</script>

这篇关于如何仅使用javascript将变量的值传递给特定的页面元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆