2009-05-20 7 views
10

Mam poniżej html:coraz href wartość od <a> tagu

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

Teraz jeśli widzisz tam jest href w div id „txtLink” czyli Australia

chcę tego na starcie strony na Te same wartości href kopiowane do powyższego znacznika div id „txtCss”, to znaczy, gdy moja strona zostanie wyświetlona mój html będzie jak poniżej:

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

proszę sugerować jakiś kod powyżej Problem

+0

Upewnij się, że kiedy piszesz kod można ustawić go w bloku kodu poprzez umieszczenie 4 spacje przed każdym wierszu lub podświetlając kodu i kliknięcie przycisku „kod”, jego jeden z 1s i 0s na tym. – UnkwnTech

+0

Zakładam, że to javascript – SilentGhost

+0

Jakiego rodzaju języka programowania używasz? PHP, ASP.NET, python, zwykły HTML ...? – Erik

Odpowiedz

11

Aktualizacja
Odpowiedź bez jQuery tutaj: https://stackoverflow.com/a/887348/11333
Powrót w 2009 roku było to całkowicie dopuszczalne, aby użyć jQuery chociaż :)

Utwórz plik js z mniej więcej tak:

$(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); 
} 

Odwołaj się zarówno do jquery, jak i tego pliku javascript w twoim html. coś takiego:

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

Czy mógłbyś to wyjaśnić, ponieważ jestem nowy w jquery –

+1

daj mi sekundę, napiszę coś o tym –

+1

Jedna rzecz, którą przegapiłem w powyższym pytaniu, że są trzy div id "txtCss", a także trzy div identyfikator "txtLink" i jeśli używam twojego kodu, to pokazuje on błąd w "$" mówiąc Błąd: $ nie jest zdefiniowany Plik źródłowy: http: // cmsstag/js/manoj .js Linia: 1 –

1

Można użyć:

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

myślę, że to powinno działać ..

+0

Drogi jose, który daje błąd, że "txtLink" jest pusty jakikolwiek pomysł –

38

Jest to najkrótsza odpowiedź bez użycia biblioteki i działa tylko coś, czego chce

+1

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

2

To jest prosty do zrozumienia kod:

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