javascript - 从 <a> 标签获取 href 值

标签 javascript html href

我有以下 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>

现在,如果您看到 div ID“txtLink”中有 href,即 Australia

我希望在页面运行时将相同的 href 值复制到上面的 div ID“txtCss”标签中,我的意思是当我的页面显示时,我的 html 将如下所示:

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

请为上述问题提出一些代码

最佳答案

这是不使用任何库的最短答案,并且只工作你想要的东西

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

关于javascript - 从 <a> 标签获取 href 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/887307/

相关文章:

javascript - 如何使 document.location.href 表现得像一个简单的链接?

python-3.x - 检查 NoneType 的变量并中断 while 循环

javascript - 如何在函数中使用时间间隔?

javascript - Elem 匹配返回所有数据,而我只需要选定的数据

javascript - Restangular POST 总是空的

html - 在 Bootstrap 中使用单选按钮更改标签字体大小时出现对齐问题

javascript - 滚轮仅滚动子内容而不滚动正文

javascript - 当鼠标悬停时传单突出显示不同颜色的标记

html - 如何使谷歌地图 iframe 的 "style"看起来很新

html - 如何使用框架集从主页菜单定向到第二个主页?