Javascript 如何通过javascript更改按钮点击时<a>标签的href

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4365246/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 12:06:28  来源:igfitidea点击:

How to change href of <a> tag on button click through javascript

javascripthrefattr

提问by Chauhan

How to change the hrefattribute value of an <a/>tag through Javascript on button click ?

如何在按钮单击时通过 Javascript更改标签的href属性值<a/>

<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>

回答by Nick Craver

Without having a href, the click will reload the current page, so you need something like this:

没有href,点击将重新加载当前页面,所以你需要这样的东西:

<a href="#" onclick="f1()">jhhghj</a>

Or prevent the scroll like this:

或者像这样防止滚动:

<a href="#" onclick="f1(); return false;">jhhghj</a>

Or return falsein your f1function and:

或者return false在你的f1函数中:

<a href="#" onclick="return f1();">jhhghj</a>

....or, the unobtrusive way:

....或者,不显眼的方式:

<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>

回答by jakobhans

Exactly what Nick Carver did there but I think it would be best if used the DOM setAttribute method.

正是 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>

It's one extra line of code but find it better structure-wise.

这是额外的一行代码,但发现它在结构上更好。

回答by atlavis

remove hrefattribute:

移除href属性:

<a id="" onclick="f1()">jhhghj</a>

if link styles are important then:

如果链接样式很重要,那么:

<a href="javascript:void(f1())">jhhghj</a>

回答by Mohsen Abasi

To have a link dynamically change on clicking it:

要在单击时动态更改链接:

<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>

回答by user3325593

<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>

回答by Arindam Roychowdhury

Here's my take on it. I needed to create a URL by collecting the value from a text box , when the user presses a Submit button.

这是我的看法。当用户按下提交按钮时,我需要通过从文本框中收集值来创建 URL。

<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>

回答by 4u.Ans

I know its bit old post. Still, it might help some one.

我知道它有点旧的帖子。不过,它可能对某些人有所帮助。

Instead of tag,if possible you can this as well.

而不是标记,如果可能的话,你也可以这样做。

 <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>`

Any comments on this?

对此有何评论?