Cómo cambiar href de etiqueta en el botón haga clic a través de javascript


¿Cómo cambiar el valor del atributo href de una etiqueta <a/> a través de Javascript al hacer clic en el botón ?

<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>
Author: Joseph Marikle, 2010-12-06

7 answers

Sin tener un href, el clic recargará la página actual, por lo que necesita algo como esto:

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

O evitar el pergamino como este:

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

O return false en su función f1 y:

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

....o, la manera discreta:

<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>
 125
Author: Nick Craver,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-12-06 10:09:29

Exactamente lo que Nick Carver hizo allí, pero creo que sería mejor si se utiliza el método DOM setAttribute.

<script type="text/javascript">
   document.getElementById("myLink").onclick = function() {
   var link = document.getElementById("abc");
   link.setAttribute("href", "xyz.php");
   return false;
   }
</script>

Es una línea adicional de código, pero lo encuentra mejor en cuanto a la estructura.

 22
Author: jakobhans,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-12-06 10:26:19

Eliminar href atributo:

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

Si los estilos de enlace son importantes entonces:

<a href="javascript:void(f1())">jhhghj</a>
 5
Author: atlavis,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-12-06 11:02:19

Para que un enlace cambie dinámicamente al hacer clic en él:

<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>
 0
Author: Mohsen Abasi,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-02-18 14:13:55
<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>
 0
Author: user3325593,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-12-04 00:00:32

Aquí está mi opinión. Necesitaba crear una URL recopilando el valor de un cuadro de texto , cuando el usuario presiona un botón Enviar.

<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>
 0
Author: Arindam Roychowdhury,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-11-12 12:29:11

Sé que es un post un poco viejo. Aún así, podría ayudar a alguien.

En lugar de etiqueta,si es posible, también puede hacer esto.

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

¿Algún comentario al respecto?

 -1
Author: 4u.Ans,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-01-05 05:36:02