2009-05-20 18 views
10

tengo a continuación html:obtener el valor href de desde <a> etiqueta

<div class="threeimages" id="txtCss"> 
<a> 
     <img alt="Australia" src="/Images/Services%20button_tcm7-9688.gif"/> 
</a>   
    <div class="text" id="txtLink">    
     <h2> 
      <a href="/partnerzone/downloadarea/school-information/australia/index.aspx">Australia</a> 
     </h2>    
     <p>Land of the sunshine!</p>   
    </div> 
</div> 

Ahora si usted ve que hay href en div id "txtLink", es decir Australia

quiero esto en el tiempo de ejecución de la página del href mismos valores se copian en la etiqueta encima del div id "txtCss", quiero decir que mi página se visualiza mi html será de la siguiente manera:

<div class="threeimages" id="txtCss"> 
<a href="/partnerzone/downloadarea/school-information/australia/index.aspx"> 
     <img alt="Australia" src="/Images/Services%20button_tcm7-9688.gif"/> 
</a>   
    <div class="text" id="txtLink">    
     <h2> 
      <a href="/partnerzone/downloadarea/school-information/australia/index.aspx">Australia</a> 
     </h2>    
     <p>Land of the sunshine!</p>   
    </div> 
</div> 

por favor, sugiera algo de código para arriba problema

+0

Asegúrese de que cuando publique el código lo configure en un bloque de código colocando 4 espacios delante de cada línea o resaltando el código y haciendo clic en el botón "código", es el que tiene los 1s y 0s en él. – UnkwnTech

+0

Supongo que es javascript – SilentGhost

+0

¿Qué tipo de lenguaje de programación está utilizando? PHP, ASP.NET, python, HTML simple ...? – Erik

Respuesta

11

actualización
una respuesta sin jQuery aquí: https://stackoverflow.com/a/887348/11333
vuelta en 2009, era perfectamente aceptable el uso de jQuery aunque :)

Crear un archivo js con algo como esto:

$(document).ready(function() { 
    $('.threeimages').each(function(){ 
    $(this).DupeHref(); 
    }); 
}); 

jQuery.fn.DupeHref = function(){  
    var target = $(this).find(".text h2 a").attr("href"); 
    $(this).find("a").attr("href", target); 
} 

Referencia tanto al jquery como a este archivo javascript en su html. Algo como esto:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml"> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
     <title>Test</title>   
    </head> 
    <body> 
     <div class="threeimages"> 
      <a> 
       <img alt="Australia" src="/Images/Services%20button_tcm7-9688.gif"/> 
      </a>   
      <div class="text">    
       <h2> 
        <a href="/partnerzone/downloadarea/school-information/australia/index.aspx">Australia</a> 
       </h2>    
       <p>Land of the sunshine!</p>   
      </div> 
     </div> 
     <div class="threeimages"> 
        <a> 
       <img alt="Belgium" src="/Images/Services%20button_tcm7-9689.gif"/> 
      </a>   
      <div class="text">    
       <h2> 
         <a href="/partnerzone/downloadarea/school-information/belgium/index.aspx">Belgium</a> 
       </h2>    
       <p>Land of beer and food!</p>   
      </div> 
     </div> 
     <script src="./content/js/jquery-1.2.6.min.js" type="text/javascript"></script> 
     <script src="./content/js/dupetargets.js" type="text/javascript"></script> 
    </body> 
</html> 
+0

Puede por favor hacerlo más claro ya que soy nuevo en jquery –

+1

dame un segundo, te escribiré algo –

+1

Una cosa que eché de menos en la pregunta anterior que hay tres div id "txtCss", así como tres ID de div "txtLink" y si estoy usando tu código, muestra un error en "$" diciendo Error: $ no está definido Archivo de origen: http: // cmsstag/js/manoj .js Línea: 1 –

1

que puede utilizar:

var txtLink = document.getElementById('txtLink'); 
var a = txtLink.getElementsByTagName('a'); 
if (a != null && a.length > 0) { 
    var setLink = txtLink.parentNode.getElementsByTagName('a'); 
    if (setLink != null && setLink.length > 0) { 
     setLink[0].href = a[0].href; 
    } 
}  

creo que esto debería funcionar ..

+0

Querido jose su error de que "txtLink" es nula alguna idea –

38

esta es la respuesta más corta sin utilizar cualquier biblioteca y trabaja único lo que quiere

var tc = document.getElementById("txtCss"); 
var ary = tc ? tc.getElementsByTagName("a") : []; 
if(ary.length >= 2) 
    ary[0].href = ary[1].href; 
+1

+1 para tc? tc.getElementsByTagName(): [] –

2

Este es un código comprensible simple:

<html> 
    <head> 
    <script language="javascript"> 
    function simpleChangeURL(){ 
     var anchors = document.getElementsByTagName('a'); 
     if(anchors != null & anchors.length > 0){ 
      anchors[0].href = anchors[1].href; 
     } 
    } 

    function simpleChangeByAustraliaURL() 
    { 
     var anchors = document.getElementsByTagName('a'); 
     var images = document.getElementsByTagName('img'); 
     var imageNeeded; 
     var anchorNeeded; 
     if(images != null){ 
      for(var i = 0; i < images.length ; i++){ 
       if (images[i].alt == 'Australia') imageNeeded = images[i]; 
      } 
     } 
     if(anchors != null){ 
      for(var j = 0; j < anchors.length ; j++){ 
       if (anchors[j].firstChild.data == 'Australia') anchorNeeded = anchors[j]; 
      } 
     } 
     if(imageNeeded != null && anchorNeeded!= null){ 
    var imageAnchor = imageNeeded.parentNode; 
    imageAnchor.href = anchorNeeded; 

} 

    } 


    </script> 
    </head> 

    <body> 
    <div class="threeimages" id="txtCss"> 
    <a> 
      <img alt="Australia" src="/Images/Services%20button_tcm7-9688.gif"/> 
    </a>   
     <div class="text" id="txtLink" >    
      <h2> 
        <a href="/partnerzone/downloadarea/school-information/australia/index.aspx">Australia</a> 
      </h2>    
      <p>Land of the sunshine!</p>   
     </div> 
    </div> 
    <script> simpleChangeByAustraliaURL();</script> 
    </body> 
    </html> 
Cuestiones relacionadas