从< a>获取href值标签 [英] getting href value of from <a> tag

查看:114
本文介绍了从< a>获取href值标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下面的html:

 < div class =threeimagesid =txtCss> 
< a>
< / a>
< div class =textid =txtLink>
< h2>
< a href =/ partnerzone / downloadarea / school-information / australia / index.aspx>澳大利亚< / a>
< / h2>
< p>阳光之地!< / p>
< / div>
< / div>

现在,如果您看到div ID为txtLink中有href,即

回到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);
}

在html中引用jquery和这个javascript文件。
类似于这样:

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

< html xmlns =http://www.w3.org/1999/xhtml>
< head>
< meta http-equiv =Content-Typecontent =text / html; charset = utf-8/>
< title>测试< /标题>
< / head>
< body>
< div class =threeimages>
< a>
< / a>
< div class =text>
< h2>
< a href =/ partnerzone / downloadarea / school-information / australia / index.aspx>澳大利亚< / a>
< / h2>
< p>阳光之地!< / p>
< / div>
< / div>
< div class =threeimages>
< a>
< img alt =比利时src =/ Images / Services%20button_tcm7-9689.gif/>
< / a>
< div class =text>
< h2>
< a href =/ partnerzone / downloadarea / school-information / belgium / index.aspx>比利时< / a>
< / h2>
< p>啤酒和食物之地!< / p>
< / div>
< / div>
< script src =./ content / js / jquery-1.2.6.min.jstype =text / javascript>< / script>
< script src =./ content / js / dupetargets.jstype =text / javascript>< / script>
< / body>
< / html>


I have below html:

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

Now if you see there is href in div ID "txtLink" i.e. Australia

I want this at the runtime of page the same href values get copied into above tag of div ID "txtCss", I mean when my page gets displayed my html will be as below:

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

please suggest some code for above problem

解决方案

Update
An answer without jquery here: https://stackoverflow.com/a/887348/11333
Back in 2009 it was perfectly acceptable to use jquery though :)

Create a js file with something like this:

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

Reference both the jquery and this javascript file in your html. Something like this:

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

这篇关于从&lt; a&gt;获取href值标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆