简体   繁体   English

显示/隐藏多个Div

[英]Show/Hide Multiple Div's

What is the best way to add another class to this script: 向此脚本添加另一个类的最佳方法是什么:

<script type="text/javascript">
    $(document).ready(function(){
     $('.carlocation').hide();
      $('#parking-options').change(function() {
        $('.carlocation').hide();
        $('#' + $(this).val()).show();
     });
    });
</script>

I am fine with the same ID displaying this classes, I am just unsure about how to add another class to this script. 我很好用显示这个类的相同ID,我只是不确定如何在这个脚本中添加另一个类。 As '.carlocation' , '.insertclass' or '.carlocation .insertclass' does nothing but break the script. 作为'.carlocation' , '.insertclass' or '.carlocation .insertclass'破坏脚本。

Thanks! 谢谢!

EDIT - The rest of the markup. 编辑 - 标记的其余部分。

I would like .carlocation and .car-position to start off as two hidden divs but in the first drop down when "Self parking" is selected that the other two selections display. 我希望.carlocation和.car-position以两个隐藏的div开始,但是在第一次下拉时选择“Self parking”,其他两个选项显示。

<li>
                    <label for="select-choice-0" class="select">Parking Method:</label>
                    <select name="select-choice-15" id="parking-options" data-theme="b" data-overlay-theme="d" data-native-menu="false" tabindex="-1">
                       <option value="">Select One</option>
                       <option value="self">Self Parking</option>
                       <option value="auto">Valet Parking</option>
                    </select>
                </li>
                <li>
                    <div id="self" class="carlocation">
                    <h1>Enter Car Location:</h1>
                    <label for="select-choice-0" class="select">Floor:</label>
                    <select name="select-choice-15" id="location-floor" data-theme="b" data-overlay-theme="d" data-native-menu="false" tabindex="-1">
                       <option value="">Floor Select</option>
                       <option value="f1">F1</option>
                       <option value="f2">F2</option>
                       <option value="f3">F3</option>
                       <option value="f4">F4</option>
                    </select>
                    </div>
                </li>
                <li>
                <div id="self" class="car-position">
                <label for="select-choice-0" class="select">Row:</label>
                <select name="select-choice-15" id="position-row" data-theme="b" data-overlay-theme="d" data-native-menu="false" tabindex="-1">
                       <option value="">Row Select</option>
                       <option value="1">1</option>
                       <option value="2">2</option>
                       <option value="3">3</option>
                       <option value="4">4</option>
                       <option value="5">5</option>
                       <option value="6">6</option>
                       <option value="7">7</option>
                </select>
                <li>

Hide your elements with CSS: 使用CSS隐藏元素:

.carlocation, .car-position {
    display: none;
}

Remove the repeated "self" id from both of the divs, and instead add the "self" value to the class attribute on both: 从两个div中删除重复的“self”id,然后将“self”值添加到两者的class属性中:

<li>
    <div class="self carlocation">
        <!-- ... -->
    </div>
</li>
<li>
    <div class="self car-position">
        <!-- ... -->
    </div>
</li>

Side Note: Your second div was missing its closing tag. 旁注:你的第二个div错过了它的结束标记。

Then bind to the change event of the form: 然后绑定到表单的change事件:

$("#parking-options").on("change", function(){
    $("div.self").toggle( $(this).val() === "self" );
});​​​​​​​​​​

This bases the visibility of all .self divs on the value of the select being "self". 这使得所有.self divs的可见性基于select为“self”的值。 If "self" is selected, all div.self items will become visible. 如果选择“self”,则所有div.self项都将变为可见。 Otherwise, they become hidden. 否则,它们就会隐藏起来。

Fiddle: http://jsfiddle.net/jonathansampson/5KJV5/ 小提琴: http//jsfiddle.net/jonathansampson/5KJV5/

Or you could slide them into view: 或者你可以将它们滑入视图:

$("#parking-options").on("change", function(){
    $(this).val() === "self"
        ? $("div.self").slideDown()
        : $("div.self").slideUp();
});​

Fiddle: http://jsfiddle.net/jonathansampson/5KJV5/2/ 小提琴: http//jsfiddle.net/jonathansampson/5KJV5/2/

Your jQuery selector can interact with multiple classes (or any other elements) by making a comma separated list within the quotes of the selector, in other words: 您的jQuery选择器可以通过在选择器的引号内创建逗号分隔列表来与多个类(或任何其他元素)进行交互,换句话说:

$('.carlocation, .insertclass, .anotherclass').hide();

Edit: Note that case sensitivity can be an issue in some cases, so '.insertclass' is not always the same as '.insertClass' - see JQuery class selectors like $(.someClass) are case sensitive? 编辑:请注意,在某些情况下区分大小写可能是一个问题,因此'.insertclass'并不总是与'.insertClass'相同 - 请参阅像$(。someClass)这样的JQuery类选择器区分大小写? for more. 更多。

It looks like you might have gotten hung up initially by not having all of your selectors in the same quotes. 看起来你最初可能因为没有将所有选择器放在相同的引号中而被挂起。 Having a space between classes as in '.carlocation .insertclass' is actually saying "select an element with the class "insertclass" that is a child of an element with class "carlocation" '.carlocation .insertclass'之间的类之间有一个空格实际上是在说“选择一个带有类”insertclass“的元素,它是一个带有”carlocation“类的元素的元素

If you are going to be interacting with the same set of elements more than once, you can optimize your code by assinging them to a variable: 如果您要多次与同一组元素进行交互,可以通过将代码分配给变量来优化代码:

var $myselection = $('.carlocation, .insertclass, .anotherclass');

(note that putting the '$' in the variable name just helps remind you that it's a jQuery object, you could name it whatever you want). (请注意,将'$'放在变量名中只会提醒您它是一个jQuery对象,您可以根据需要命名它。

You can now use any of the normal jQuery methods on $myselection: 您现在可以在$ myselection上使用任何常规jQuery方法:

$myselection.hide();
$myselection.show();

or use it later (so long as the variable is accessible within the scope that you're looking for it, which wouldn't be a problem in your initial example). 或者稍后使用它(只要变量可以在您正在寻找的范围内访问,这在您的初始示例中不会出现问题)。

To select multiple selectors try this- 要选择多个选择器,请尝试以下方法

$("selector1,selector2")

It will definetly work. 它肯定会起作用。 For more information visit jQuery selectors reference. 有关更多信息,请访问jQuery选择器参考。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM