[英]Return value from Javascript to HTML <a href> tag
我有这样的html代码:
<a id="abc_1" name="link_1" href=""
onclick="updateLink(this.id);">Test Link</a>
我的Javascript函数是这样的:
function updateLink(a){
var newurl = document.location.href "+" + a.valueOf();
return newurl;
}
单击链接后,我想将用户定向到此newurl。 怎么做? 注意:我已经看到了使用<div>
标记的示例,但是我需要在<a href>
标记中使用它。
一种方法是从点击处理程序更改href属性
window.updateLink = function (a){ // This won't work in this example because this frame // is set 'X-Frame-Options' to 'SAMEORIGIN'. // However, the error message displayed in the console is indication // that it indeed tried to navigate to the URL I gave it a.setAttribute('href', 'http://www.google.com?q=' + a.id); }
<a href="#" id="http://www.google.com" onclick="updateLink(this)" >Test</a>
我假定锚属性ID或名称包含有关新URL的信息。
看看这个例子:
<a href="#" id="abc_1" name="link_1" onclick="updateLink(this)" >Test Link</a>
<script>
function updateLink(a) {
var newurl = "";
//You could retrieve the object name
newurl = a.getAttribute("name");
//Or, retrieve the id
newurl = a.getAttribute("id");
//redirect
location.href = newurl;
}
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.