Man*_*ngh 10 html javascript 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>
Run Code Online (Sandbox Code Playgroud)
现在,如果你看到div ID"txtLink"中有href,即澳大利亚
我想在页面的运行时将相同的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>
Run Code Online (Sandbox Code Playgroud)
请为上述问题提出一些代码
Tol*_*rak 38
这是没有使用任何库的最短答案,只能用于你想要的东西
var tc = document.getElementById("txtCss");
var ary = tc ? tc.getElementsByTagName("a") : [];
if(ary.length >= 2)
ary[0].href = ary[1].href;
Run Code Online (Sandbox Code Playgroud)
Bor*_*ens 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);
}
Run Code Online (Sandbox Code Playgroud)
在你的html中引用jquery和这个javascript文件.像这样的东西:
<!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>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
98564 次 |
| 最近记录: |