简体   繁体   English

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

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

How to change the href attribute value of an <a/> tag through Javascript on button click ?如何在按钮单击时通过 Javascript 更改<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>

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 false in your f1 function and:或者在你的f1函数中return false并且:

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

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.这是额外的一行代码,但发现它在结构上更好。

remove href attribute:删除href属性:

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

if link styles are important then:如果链接样式很重要,那么:

<a href="javascript:void(f1())">jhhghj</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>

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>

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>
<script type="text/javascript">
  function f1(mHref)
  {
    document.getElementById("abc").href=mHref; 
  }
</script>

<a href="" id="abc">jhg</a>
<button onclick="f1("dynamicHref")">Change HREF</button>

Just give the dynamic HREF in Paramters

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?对此有何评论?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM