1

我创建了这个栏,单击时会打开幻灯片。但是,我只希望它在单击 + 时打开。我有 + 超链接,但我不知道如何使滑块只响应 + 并使侧边栏恢复正常(现在它在内容 div 下方移动。请帮助。

    <!DOCTYPE HTML>

<html>
    <head>
        <title>Title</title>
        <link rel="stylesheet" href="style.css">

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js">   </script>
    </head>

    <body>

    <div id="wrapper">
        <div id="nav">
        <div id="menu-expand">
        <a href="#" class="closed"><span>+</span></a>
        </div>
        </div>
            <div id="content"></div>
    </div>

    <!-- Scripts -->

<script>
    $(function() {
    $('#nav').click(function() {
        var leftMargin = ($(this).css('margin-left') === '0px') ? '-150px' : '0px';
        $(this).animate({ 'margin-left' : leftMargin }, 500);
    });
});

</script>
    </body>
</html>
4

1 回答 1

0

点击事件在 DOM 中“冒泡”,在每个父元素上触发,除非您使用ev.stopPropagation()

$('#nav').click(function(ev) {
    ev.stopPropagation();
    var leftMargin = ($(this).css('margin-left') === '0px') ? '-150px' : '0px';
    $(this).animate({ 'margin-left' : leftMargin }, 500);
});
于 2012-08-13T19:13:24.620 回答