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