如何通过javascript来改变button上的<a>标签的href
如何通过点击button来改变一个<a/>标签的href属性值? 
 <script type="text/javascript"> function f1() { document.getElementById("abc").href="xyz.php"; } </script> <a href="" id="abc">jhg</a> <a href="" id="" onclick="f1()">jhhghj</a> 
	
 没有href ,点击将重新加载当前页面,所以你需要这样的东西: 
 <a href="#" onclick="f1()">jhhghj</a> 
或者像这样防止滚动:
 <a href="#" onclick="f1(); return false;">jhhghj</a> 
 或者在你的f1函数中return false ,并且: 
 <a href="#" onclick="return f1();">jhhghj</a> 
….或者不显眼的方式:
 <a href="#" id="abc">jhg</a> <a href="#" id="myLink">jhhghj</a> <script type="text/javascript"> document.getElementById("myLink").onclick = function() { document.getElementById("abc").href="xyz.php"; return false; }; </script> 
究竟是Nick Carver在那里做了什么,但是我认为最好是使用DOM setAttribute方法。
 <script type="text/javascript"> document.getElementById("myLink").onclick = function() { var link = document.getElementById("abc"); link.setAttribute("href", "xyz.php"); return false; } </script> 
这是一个额外的代码行,但find更好的结构明智。
 删除href属性: 
 <a id="" onclick="f1()">jhhghj</a> 
如果链接样式很重要,那么:
 <a href="javascript:void(f1())">jhhghj</a> 
我知道它的旧post。 不过,这可能会有所帮助。
如果可能的话,也可以这样做,而不是标签。
  <script type="text/javascript"> function IsItWorking() { // Do your stuff here ... alert("YES, It Works...!!!"); } </script> `<asp:HyperLinkID="Link1"NavigateUrl="javascript:IsItWorking();"` `runat="server">IsItWorking?</asp:HyperLink>` 
对此有何评论?
要点击链接来dynamic更改链接:
 <input type="text" id="emailOfBookCustomer" style="direction:RTL;"></input> <a onclick="this.href='<%= request.getContextPath() %>/Jahanpay/forwardTo.jsp?handle=<%= handle %>&Email=' + document.getElementById('emailOfBookCustomer').value;" href=''> A dynamic link </a> 
 <a href="#" id="a" onclick="ChangeHref()">1.Change 2.Go</a> <script> function ChangeHref(){ document.getElementById("a").setAttribute("onclick", "location.href='http://religiasatanista.ro'"); } </script> 
这是我的承诺。 当用户按下“提交”button时,我需要通过从文本框中收集值来创buildURL。
 <html> <body> Hi everyone <p id="result"></p> <textarea cols="40" id="SearchText" rows="2"></textarea> <button onclick="myFunction()" type="button">Submit!</button> <script> function myFunction() { var result = document.getElementById("SearchText").value; document.getElementById("result").innerHTML = result; document.getElementById("abc").href="http://arindam31.pythonanywhere.com/hello/" + result; } </script> <a href="#" id="abc">abc</a> </body> <html>