【问题标题】:Find element via parameterized css selector通过参数化 css 选择器查找元素
【发布时间】:2014-02-11 12:14:58
【问题描述】:

我正在使用带有多个超链接的 jQuery 构建一个菜单,我想要的是在单击超链接时显示/隐藏包含子菜单的 div。

我尝试了以下方法:

$("div#divmnu" + mnuidx).show();
$("div#divmnu" + mnuidx).animate({ height: '300px' }, 300);

但它不起作用!

谁能帮帮我?

【问题讨论】:

  • 请分享您的 HTML 吗?

标签: jquery elementname


【解决方案1】:

这行得通:

<html>
<head>
<title>JQuery Show/Hide w/ Animate</title>
<script src="js/jquery-1.8.2.js"></script>
<script src="js/jquery-ui.js"></script>
<script>
    function show(button) {
        var idx = button.attr("id").substring(1);
        $("div#menu" + idx).show();
        $("div#menu" + idx).animate({ height: '300px' }, 300);
    }
    function hide(button) {
        var idx = button.attr("id").substring(1);
        $("div#menu" + idx).animate({ height: '20px' }, 300, "swing", function() { $("div#menu" + idx).hide(); });
    }
    $(document).ready(function() {
        $("div#menu1").hide();
        $("div#menu2").hide();
        $("#s1").click(function() { show($(this)); });
        $("#s2").click(function() { show($(this)); });
        $("#h1").click(function() { hide($(this)); });
        $("#h2").click(function() { hide($(this)); });
    });
</script>
</head>
<body>
<div id="menu1" style="font-size: xx-large;">Menu 1</div>
<div id="menu2" style="font-size: xx-large;">Menu 2</div>
<div><button id="s1">Show 1</button><button id="s2">Show 2</button>
     <button id="h1">Hide 1</button><button id="h2">Hide 2</button></div>
</body>
</html>

【讨论】:

    猜你喜欢
    • 2019-04-30
    • 1970-01-01
    • 2021-11-21
    • 2020-12-12
    • 2023-03-16
    • 1970-01-01
    • 2018-10-22
    • 2019-07-20
    • 1970-01-01
    相关资源
    最近更新 更多