如何在按钮上通过javascript修改标签的href ?

时间:2022-11-20 15:16:34

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

如何在按钮点击时通过Javascript修改标签的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>

7 个解决方案

#1


119  

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函数中返回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>

#2


22  

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.

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

#3


5  

remove href attribute:

删除href属性:

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

if link styles are important then:

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

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

#4


0  

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?

任何评论吗?

#5


0  

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>

#6


0  

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

#7


0  

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>

#1


119  

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函数中返回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>

#2


22  

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.

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

#3


5  

remove href attribute:

删除href属性:

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

if link styles are important then:

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

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

#4


0  

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?

任何评论吗?

#5


0  

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>

#6


0  

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

#7


0  

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>