jquery 如何获取当前点击的上一个特定元素
<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的啊?
------解决方案--------------------<!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>
这样试试
------解决方案--------------------看错文档结构.你的缩进没有道理
<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)">