gpt4 book ai didi

javascript - 调用 ul 中下一个元素的函数

转载 作者:行者123 更新时间:2023-11-28 08:27:16 26 4
gpt4 key购买 nike

尝试了解 Javascript 和 HTML5 的工作原理。我有一个导航,当您按第一部分时,我会尝试显示内容 1,当您按第二部分时……等等。我尝试了一些方法,我显然没有正确理解 .next,但我只能让它显示第一个元素,但是当我按第二部分时,内容二不显示。我想我想象它是错误的,而不是 .first(),.next 对我来说很有意义,因为我觉得这只会得到下一个元素。无论如何,这是一些代码,提前致谢。

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<title>Assignment 6 | jQuery Usability </title>

<link rel="stylesheet" href="stylesheet.css">


<!-- This matches the CSS width to the device width, and prevents forced overview without disabling zooming -->
<meta name="viewport" content="width=device-width, initial-scale=1">

<!-- Helpful for older Blackberries -->
<meta name="HandheldFriendly" content="True">
<!-- Helpful for older IE mobile -->
<meta name="MobileOptimized" content="320">
<!-- Apple iOS-specific -->
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">


<!-- enforce CSS3 media queries in IE 8 and older -->
<!--[if lt IE 9]>
<script src="http://css3-mediaqueries-js.googlecode.com/svn/trunk/css3-mediaqueries.js"></script>
<![endif]-->
<!-- Note: this won't work when testing locally; you'll need to upload your files to a server to test it in IE -->


<!-- HTML5shiv, for IE 6, 7 and 8 -->
<!--[if lte IE 9]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js" type="text/javascript"></script>
<![endif]-->


<!-- jQuery -->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>

<script>
$(document).ready(function() {

$('ul').hide();

$('a.btnDown').click(function() {

$('nav>ul').fadeIn(200);

}); //closes a.btnDown

$('a.contentDown').click(function() {
$('body>aside>ul').first().fadeIn(200);

}); //closes contentDown





}); //closes .ready()
</script>

</head>

<body>

<nav>
<h1><a class="btnDown" href="#"> Main Menu </a></h1>
<ul>
<li><a class="contentDown" href="#"> Part One </a></li>

<li><a class="contentDown" href="#"> Part Two </a></li>

<li><a class="contentDown" href="#"> Part Three </a></li>

<li><a class="contentDown" href="#"> Student Notes 1 </a></li>

<li><a class="contentDown" href="#"> Student Notes 2 </a></li>

<li><a class="contentDown" href="#"> Student Notes 3</a></li>

</ul>

</nav>

<aside>

<ul>
<li> Content 1 </li>
</ul>

<ul>
<li> Content 2 </li>
</ul>

<ul>
<li> Content 3 </li>
</ul>

<ul>
<li> Content 4 </li>
</ul>

<ul>
<li> Content 5 </li>
</ul>


<ul>
<li> Content 6 </li>
</ul>
</aside>

<figure>

<!-- PUT TV IMAGE HERE -->
</figure>




</body>
</html>

最佳答案

对于给定的标记,最简单的解决方案是使用元素的索引,如下所示

$(document).ready(function () {

$('ul').hide();

$('a.btnDown').click(function () {
$('nav>ul').fadeIn(200);
return false;
}); //closes a.btnDown

//all the content elements
var $suls = $('body>aside>ul');
var $as = $('a.contentDown').click(function () {
//hide visible content item
$suls.filter(':visible').hide();
//display the content item in the same position as the clicked contentDown
$suls.eq($as.index(this)).fadeIn(200);
}); //closes contentDown
}); //closes .ready()

演示:Fiddle

关于javascript - 调用 ul 中下一个元素的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22256703/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com