2009-05-20 14 views
10

से मैं एचटीएमएल नीचे है:की href मूल्य हो रही <a> टैग

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

अब अगर आप देख वहाँ में div आईडी href है "txtLink" यानी Australia

मैं इस पेज के क्रम में चाहते

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

ऊपर के लिए कुछ कोड का सुझाव दें: एक ही href मूल्यों div आईडी "txtCss" के ऊपर टैग में कॉपी हो, मैं जब मेरे पेज प्रदर्शित किया जाता मेरी एचटीएमएल के रूप में नीचे हो जाएगा मतलब समस्या

+0

सुनिश्चित करें कि जब आप कोड पोस्ट आप इसे एक कोड ब्लॉक में या तो प्रत्येक पंक्ति के सामने 4 स्थानों रखकर या कोड पर प्रकाश डाला और "कोड" बटन पर क्लिक करके सेट करें, 1s के साथ अपने एक और इस पर 0s। – UnkwnTech

+0

मुझे लगता है कि यह जावास्क्रिप्ट – SilentGhost

+0

आप किस प्रकार की प्रोग्रामिंग भाषा का उपयोग कर रहे हैं? PHP, एएसपी.नेट, पायथन, सादा एचटीएमएल ...? – Erik

उत्तर

11

अद्यतन
यहाँ jQuery के बिना एक उत्तर: https://stackoverflow.com/a/887348/11333
2009 में वापस यह पूरी तरह jQuery का उपयोग करना स्वीकार्य था, हालांकि :)

कुछ इस तरह के साथ एक js फ़ाइल बनाएँ:

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

अपने एचटीएमएल में jquery और यह जावास्क्रिप्ट फ़ाइल दोनों का संदर्भ लें। कुछ इस तरह:

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

में प्राप्त कर सकूं, तो कृपया इसे और अधिक स्पष्ट कर सकते हैं क्योंकि मैं jquery –

+1

पर एक नया हूं, मैं आपको कुछ लिखूंगा –

+1

एक बात जो मुझे उपरोक्त प्रश्न में याद आई है कि तीन हैं div id "txtCss" के साथ-साथ तीन div ID "txtLink" और यदि मैं आपका कोड उपयोग कर रहा हूं तो यह "$" पर त्रुटि दिखा रहा है त्रुटि: $ परिभाषित नहीं किया गया है स्रोत फ़ाइल: http: // cmsstag/js/manoj .js रेखा: 1 –

1

आप उपयोग कर सकते हैं:

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

मुझे लगता है कि यह काम करना चाहिए ..

+0

प्रिय जोस ने यह त्रुटि दे दी है कि "txtLink" tc के लिए –

38

यह किसी भी पुस्तकालय का उपयोग किए बिना कम से कम जवाब है और केवल एक चीज से काम करता है कि आप क्या

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

+1 को कोई विचार नहीं है? tc.getElementsByTagName(): [] –

2

यह एक साधारण समझने योग्य कोड है:

<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> 
संबंधित मुद्दे