2009-05-20 36 views
10

tôi có dưới đây html:nhận được giá trị href từ <a> thẻ

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

Bây giờ nếu bạn thấy có href trong div ID "txtLink" tức là Australia

Tôi muốn điều này ở thời gian chạy của trang giá trị href cùng được sao chép vào thẻ trên của div ID "txtCss", ý tôi là khi trang web của tôi được hiển thị html của tôi sẽ như sau:

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

xin đề xuất một số mã cho trên vấn đề

+0

Đảm bảo rằng khi bạn đăng mã, bạn đặt mã trong một khối mã bằng cách đặt 4 dấu cách trước mỗi dòng hoặc bằng cách làm nổi bật mã và nhấp vào nút "mã" 0 trên đó. – UnkwnTech

+0

Tôi cho rằng đó là javascript – SilentGhost

+0

Bạn đang sử dụng loại ngôn ngữ lập trình nào? PHP, ASP.NET, python, HTML thuần túy ...? – Erik

Trả lời

11

Cập nhật
Một câu trả lời mà không cần jquery ở đây: https://stackoverflow.com/a/887348/11333
Trở lại năm 2009 là hoàn toàn chấp nhận sử dụng jquery dù :)

Tạo một file js với một cái gì đó như thế này:

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

Tham chiếu cả tệp jquery và tệp javascript này trong html của bạn. Something như thế này:

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

bạn có thể xin vui lòng làm cho nó rõ ràng hơn như tôi mới để jquery –

+1

gimme một giây, tôi sẽ viết cho bạn một cái gì đó lên –

+1

Một điều tôi bị mất trong câu hỏi trên có ba div id "txtCss" cũng như ba div ID "txtLink" và nếu tôi đang sử dụng mã của bạn thì nó hiển thị lỗi trên "$" nói Lỗi: $ không được xác định Tệp nguồn: http: // cmsstag/js/manoj .js Đường: 1 –

1

Bạn có thể sử dụng:

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

Tôi nghĩ rằng điều này sẽ làm việc ..

+0

Kính gửi câu trả lời lỗi của nó rằng "txtLink" không có ý tưởng nào –

38

này là câu trả lời ngắn nhất mà không sử dụng bất kỳ thư viện và các công trình chỉ điều gì bạn muốn

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

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

2

Đây là mã đơn giản dễ hiểu:

<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> 
Các vấn đề liên quan