<dt>Wy</dt>
<dd><a href="javascript:void(0)">Wyoming</a></dd>
<dt>Washington</dt>
<dd><a href="javascript:void(0)">Bellingham</a></dd>
<dd><a href="javascript:void(0)">Moses Lake</a></dd>
<dd><a href="javascript:void(0)">Olympia</a></dd>
<dd><a href="javascript:void(0)">Seattle</a></dd>
<dd><a href="javascript:void(0)">Spokane</a></dd>
<dd><a href="javascript:void(0)">Tri-Cities (WA)</a></dd>
<dd><a href="javascript:void(0)">Walla Walla</a></dd>
<dd><a href="javascript:void(0)">Yakima</a></dd>
点击任一个dd里的链接,能获取其往上数的dt的文本. 例如点击seattle获得washington, 点击wyoming获得wy
jquery的prev()是获取同级别的元素的,像我这种如何获取dt的啊?
6 个解决方案
#1
直接 .parent("dt");
#2
$("dd").children("a").click(function(){
var states=$(this).parent("dt").text();
alert(states);
貌似不行啊?还是我代码错了?
#3
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script type="text/javascript">
function init(){
var dd=document.getElementsByTagName("dd");
for(var i=0;i<dd.length;i++){
dd[i].onclick=showParent;
}
}
function showParent(){
var dt=this.previousSibling;
while(dt.tagName!='DT'){
dt=dt.previousSibling;
}
if(dt){
alert(dt.innerHTML);
}
}
window.onload=init;
</script>
</head>
<body>
<dt>Wy</dt>
<dd><a href="javascript:void(0)">Wyoming</a></dd>
<dt>Washington</dt>
<dd><a href="javascript:void(0)">Bellingham</a></dd>
<dd><a href="javascript:void(0)">Moses Lake</a></dd>
<dd><a href="javascript:void(0)">Olympia</a></dd>
<dd><a href="javascript:void(0)">Seattle</a></dd>
<dd><a href="javascript:void(0)">Spokane</a></dd>
<dd><a href="javascript:void(0)">Tri-Cities (WA)</a></dd>
<dd><a href="javascript:void(0)">Walla Walla</a></dd>
<dd><a href="javascript:void(0)">Yakima</a></dd>
</body>
</html>
这样试试
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script type="text/javascript">
function init(){
var dd=document.getElementsByTagName("dd");
for(var i=0;i<dd.length;i++){
dd[i].onclick=showParent;
}
}
function showParent(){
var dt=this.previousSibling;
while(dt.tagName!='DT'){
dt=dt.previousSibling;
}
if(dt){
alert(dt.innerHTML);
}
}
window.onload=init;
</script>
</head>
<body>
<dt>Wy</dt>
<dd><a href="javascript:void(0)">Wyoming</a></dd>
<dt>Washington</dt>
<dd><a href="javascript:void(0)">Bellingham</a></dd>
<dd><a href="javascript:void(0)">Moses Lake</a></dd>
<dd><a href="javascript:void(0)">Olympia</a></dd>
<dd><a href="javascript:void(0)">Seattle</a></dd>
<dd><a href="javascript:void(0)">Spokane</a></dd>
<dd><a href="javascript:void(0)">Tri-Cities (WA)</a></dd>
<dd><a href="javascript:void(0)">Walla Walla</a></dd>
<dd><a href="javascript:void(0)">Yakima</a></dd>
</body>
</html>
这样试试
#4
#5
看错文档结构.你的缩进没有道理
<dt>Wy</dt>
<dd><a href="javascript:void(0)">Wyoming</a></dd>
<dt>Washington</dt>
<dd><a href="javascript:void(0)">Bellingham</a></dd>
<dd><a href="javascript:void(0)">Moses Lake</a></dd>
<dd><a href="javascript:void(0)">Olympia</a></dd>
<dd><a href="javascript:void(0)">Seattle</a></dd>
<dd><a href="javascript:void(0)">Spokane</a></dd>
<dd><a href="javascript:void(0)">Tri-Cities (WA)</a></dd>
<dd><a href="javascript:void(0)">Walla Walla</a></dd>
<dd><a href="javascript:void(0)">Yakima</a></dd>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script type="text/javascript">
<!--
$("dd").children("a").click(function(){
var states=$(this).parent("dd").prevAll("dt")[0];
alert($(states).text());
});
//-->
</script>
#6
可以咯,谢谢!
#1
直接 .parent("dt");
#2
$("dd").children("a").click(function(){
var states=$(this).parent("dt").text();
alert(states);
貌似不行啊?还是我代码错了?
#3
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script type="text/javascript">
function init(){
var dd=document.getElementsByTagName("dd");
for(var i=0;i<dd.length;i++){
dd[i].onclick=showParent;
}
}
function showParent(){
var dt=this.previousSibling;
while(dt.tagName!='DT'){
dt=dt.previousSibling;
}
if(dt){
alert(dt.innerHTML);
}
}
window.onload=init;
</script>
</head>
<body>
<dt>Wy</dt>
<dd><a href="javascript:void(0)">Wyoming</a></dd>
<dt>Washington</dt>
<dd><a href="javascript:void(0)">Bellingham</a></dd>
<dd><a href="javascript:void(0)">Moses Lake</a></dd>
<dd><a href="javascript:void(0)">Olympia</a></dd>
<dd><a href="javascript:void(0)">Seattle</a></dd>
<dd><a href="javascript:void(0)">Spokane</a></dd>
<dd><a href="javascript:void(0)">Tri-Cities (WA)</a></dd>
<dd><a href="javascript:void(0)">Walla Walla</a></dd>
<dd><a href="javascript:void(0)">Yakima</a></dd>
</body>
</html>
这样试试
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script type="text/javascript">
function init(){
var dd=document.getElementsByTagName("dd");
for(var i=0;i<dd.length;i++){
dd[i].onclick=showParent;
}
}
function showParent(){
var dt=this.previousSibling;
while(dt.tagName!='DT'){
dt=dt.previousSibling;
}
if(dt){
alert(dt.innerHTML);
}
}
window.onload=init;
</script>
</head>
<body>
<dt>Wy</dt>
<dd><a href="javascript:void(0)">Wyoming</a></dd>
<dt>Washington</dt>
<dd><a href="javascript:void(0)">Bellingham</a></dd>
<dd><a href="javascript:void(0)">Moses Lake</a></dd>
<dd><a href="javascript:void(0)">Olympia</a></dd>
<dd><a href="javascript:void(0)">Seattle</a></dd>
<dd><a href="javascript:void(0)">Spokane</a></dd>
<dd><a href="javascript:void(0)">Tri-Cities (WA)</a></dd>
<dd><a href="javascript:void(0)">Walla Walla</a></dd>
<dd><a href="javascript:void(0)">Yakima</a></dd>
</body>
</html>
这样试试
#4
#5
看错文档结构.你的缩进没有道理
<dt>Wy</dt>
<dd><a href="javascript:void(0)">Wyoming</a></dd>
<dt>Washington</dt>
<dd><a href="javascript:void(0)">Bellingham</a></dd>
<dd><a href="javascript:void(0)">Moses Lake</a></dd>
<dd><a href="javascript:void(0)">Olympia</a></dd>
<dd><a href="javascript:void(0)">Seattle</a></dd>
<dd><a href="javascript:void(0)">Spokane</a></dd>
<dd><a href="javascript:void(0)">Tri-Cities (WA)</a></dd>
<dd><a href="javascript:void(0)">Walla Walla</a></dd>
<dd><a href="javascript:void(0)">Yakima</a></dd>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script type="text/javascript">
<!--
$("dd").children("a").click(function(){
var states=$(this).parent("dd").prevAll("dt")[0];
alert($(states).text());
});
//-->
</script>
#6
可以咯,谢谢!