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

标签 javascript jquery html

尝试了解 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/

相关文章:

javascript - Jest 不能使用 js 文件中的 HOC,但可以正确使用 tsx 文件中的 HOC

javascript - 两个字符串的比较在脚本中不起作用

javascript - 无法避免 'onbeforeunload' 事件的默认警告

javascript - 选中第一组复选框后,输入提交将无法禁用

javascript - jQuery:价格不会更新 - 它不会正确减去(使用 switch 语句)

css - 当内容大于 div1 的大小时放置 div2

javascript - 使用chrome和javascript另存为pdf

javascript - 来自数组的 AutoSuggest

javascript - 如何检测 css 模态之外的点击?

php - 如何通过 php 页面检索数据库值到 jquery 自动完成框