当前位置: 代码迷 >> Web前端 >> Jquery查寻父级元素
  详细解决方案

Jquery查寻父级元素

热度:293   发布时间:2012-09-03 09:48:39.0
Jquery查找父级元素
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<title>MyHtml.html</title>

		<script type="text/javascript"
			src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
		<script type="text/javascript">
		$(document).ready(function () {
			var as = $("a");
			$.each(as, function(i, n){
				$(n).click(function(){
					if($(this).parent().attr("class")=="closed")  
						$(this).parent().attr("class","opened");
					else
						$(this).parent().attr("class","closed");  
				});  
			});
		});
		</script>
	</head>

	<body>
		<ul>
			<li class="opened">
				<span class="liststyle"></span><a href="#">搜索1</a>
				<p class="clear"></p>
			</li>
			<li class="opened">
				<span class="liststyle"></span><a href="#">搜索2</a>
				<p class="clear"></p>
			</li>
			<li class="opened">
				<span class="liststyle"></span><a href="#">搜索3</a>
				<p class="clear"></p>
			</li>
			<li class="opened">
				<span class="liststyle"></span><a href="#">搜索4</a>
				<p class="clear"></p>
			</li>
		</ul>
	</body>
</html>

?2.

$("a").click(function(){
    $(this).parent().attr("class","closed");
)

?

3.

$("a").click(function(){ 
  $(this).closest("li").removeClass("opened").addClass("closed"); 
});

4.

if ($(this).parent().first().attr("class") == "MyName") {
  temp = temp+"(Normal)";
  } 

alert($("#show").attr("class").indexOf("b"));
alert($("#show2").attr("class") == "myCls");
alert($("div:eq(0)").attr("class") == "myCls"); 

<div id="show" class="a b c"></div>
<div id="show2" class="myCls"></div>

?

5.

1.attr(name)
2.$(id)[index].className

?

?

?

  相关解决方案