【问题标题】:JQuery .not() function not working correctlyJQuery .not() 函数无法正常工作
【发布时间】:2014-08-19 15:31:24
【问题描述】:

所以我在使用 jQuery .not() 函数时遇到了问题。

所有 div 都通过切换正常进行折叠,但问题在于关闭所有其他 div 类。如果您打开了第一个 div,点击任何其他 div 会关闭它,但如果您先点击任何其他 div,然后再点击另一个,它们不会关闭。

这里是代码

function hideNot(id) {
    $("#teamMemberDescription").not("." + id).hide();
}

$("#teamMember.paulturner").click(function(){
    hideNot("paulturner");
    $("#teamMemberDescription.paulturner").toggle(500);
});

$("#teamMember.paulblake").click(function(){
    hideNot("paulblake");
    $("#teamMemberDescription.paulblake").toggle(500);
});

$("#teamMember.bobhook").click(function(){
    hideNot("bobhook");
    $("#teamMemberDescription.bobhook").toggle(500);
});

$("#teamMember.yvonnemclean").click(function(){
    hideNot("yvonnemclean");
    $("#teamMemberDescription.yvonnemclean").toggle(500);
});

这是html

<div id="standardContainer" class="teamPage">
        <div id="standardContent">

                <h1 class="entry-title"><whiteblocker>MEET THE TEAM</whiteblocker></h1>

                <div id="teamBanner">
                    <img src="<?php the_field('team_group_image');?>" width="100%">
                </div>

                <div id="teamDescription">
                    <h2>PREVENTEC - WHO WE ARE</h2>
                    <?php the_field('team_description');?>
                </div>

                <div id="teamMembers">
                    <?php if(get_field('team_members')): ?>         
                        <?php while(the_repeater_field('team_members')): ?>
                            <?php $memberName = get_sub_field('member_name');?>
                            <?php $memberName = str_replace(" ","", $memberName);?>
                            <?php $memberName = strtolower($memberName);?>

                            <div id="teamMember" class="<?php echo $memberName;?>">
                                <img src="<?php the_sub_field('member_photo');?>">
                                <div class="teamMemberDetails">
                                    <h3><?php the_sub_field('member_name');?></h3>
                                    <h4><?php the_sub_field('member_position');?></h4>
                                </div>
                            </div>

                        <?php endwhile; ?>
                    <?php endif;?>  
                </div>  

        </div>
    </div>

    <div id="teamDetailsContainer">
        <div id="teamDetailsContent">
            <?php if(get_field('team_members')): ?>         
                <?php while(the_repeater_field('team_members')): ?>
                    <?php $memberName = get_sub_field('member_name');?>
                    <?php $memberName = str_replace(" ","", $memberName);?>
                    <?php $memberName = strtolower($memberName);?>

                    <div id="teamMemberDescription" class="<?php echo $memberName;?>">
                        <h1><?php the_sub_field('member_name');?></h1>
                        <?php the_sub_field('member_description');?>
                    </div>
                <?php endwhile; ?>
            <?php endif;?>
        </div>
    </div>

任何帮助将不胜感激

【问题讨论】:

  • 显示您的 HTML。
  • 您在选择元素时遇到问题,但甚至没有显示文档结构?您有 UI 行为问题,但甚至没有包含功能演示?
  • 你有多个具有相同 id 的 div?!
  • 元素ids 必须是唯一的。看起来您有多个 ID 为 teamMemberteamMemberDescription 的元素。首先将它们更改为类。
  • 请发布浏览器看到的文档,而不是您的 PHP 代码。

标签: javascript jquery function


【解决方案1】:

只是猜测:你的结构应该是

// HTML
<div id="description">
    <div id="paul">...</div>
    <div id="...">...</div>
</div>

// JQUERY
function hideNot(name) {
    $("#description > div").not("#" + name).hide();
}

【讨论】:

  • 答案有什么问题?它只是有效,并且在所有更新之前与原始问题完全匹配!帮助没有提出正确问题的人真的很有趣!
猜你喜欢
  • 1970-01-01
  • 2020-08-26
  • 2013-11-26
  • 1970-01-01
  • 2021-12-10
  • 1970-01-01
  • 1970-01-01
  • 2017-03-26
  • 1970-01-01
相关资源
最近更新 更多