简体   繁体   English

Javascript显示/隐藏多个DIV

[英]Javascript show/hide multiple DIV's

I'm in need of a bit of help. 我需要一些帮助。 I have a current script that switches div's between being visible and hidden depending on a dropdown selector, it works as it was originally designed absolutely fine. 我有一个当前的脚本,根据下拉选择器切换div在可见隐藏之间,它的工作方式与最初的设计完全一样。

The problem i have is that i need to modify it to change more than 1 div on the page. 我的问题是我需要修改它以在页面上更改超过1 div。 Currently i'm using the same ID for the div's but only the first item on the page is updated. 目前我正在为div使用相同的ID,但只更新页面上的第一项。 Reading over the JS this makes sense, but i can't figure out how to modify it to get the desired result? 阅读JS这是有道理的,但我无法弄清楚如何修改它以获得所需的结果?

Javascript: 使用Javascript:

var lastDiv = "";
var lastProd = "";
function showDiv(divName, productID) {
    if (productID == lastProd) {
        $("#"+lastDiv).hide();
        $("#"+divName).fadeIn(".visible-div-"+productID);
    } 
    else {
        $(".visible-div-"+productID).hide();
        $("#"+divName).fadeIn(".visible-div-"+productID);
    }
    lastProd = productID;
    lastDiv = divName;
} 

The selector: 选择器:

<select onchange="showDiv('pxo_'+this.value,2);" name="pre_xo_id">
<option value="3">Blue - £120.00</option>
<option value="4">Red - £120.00</option>
<option value="5">Yellow - £120.00</option>

The DIV's: DIV的:

<div id="pxo_3" class="visible-div-2" style="display: none;">RED</div>
<div id="pxo_4" class="hidden-div visible-div-2" style="display: none;">BLUE</div>
<div id="pxo_5" class="hidden-div visible-div-2" style="display: block;">YELLOW</div>

<div id="pxo_3" class="visible-div-2" style="display: none;">1 In Stock</div>
<div id="pxo_4" class="hidden-div visible-div-2" style="display: none;">1 In Stock</div>
<div id="pxo_5" class="hidden-div visible-div-2" style="display: none;">0 In Stock</div>

id 's must be unique, that's why only the first item is being update. id必须是唯一的,这就是为什么只有第一个项目正在更新。 You may put those values to class instead to allow multiple selection. 您可以将这些值放到class以允许多个选择。

First you can not use one id for morethan one element.They must be unique.Apply same css class to those elements. 首先,你不能使用一个id多于一个元素。它们必须是唯一的。对这些元素应用相同的css类。 We can use same class instead to allow multiple selection. 我们可以使用相同的类来允许多个选择。

IDs are supposed to be used for only a single element on the page. ID应该仅用于页面上的单个元素。 You want to use css selectors. 你想使用css选择器。

Thank you for the help all, I have modified the JS to look for both ID and Class as i am unable to change part of the code due to it being protected by ioncube. 感谢大家的帮助,我修改了JS来查找ID和Class,因为我无法更改部分代码,因为它受到了ioncube的保护。

This seems to have the desired result: 这似乎有所期望的结果:

var lastDiv = "";
var lastProd = ""; 
function showDiv(divName, productID) {
    if (productID == lastProd) {
        $("#"+lastDiv).hide();
        $("#"+divName).fadeIn(".visible-div-"+productID);
        $("."+lastDiv).hide();
        $("."+divName).fadeIn(".visible-div-"+productID);
    } else {
        $(".visible-div-"+productID).hide();
        $("#"+divName).fadeIn(".visible-div-"+productID);
        $(".visible-div-"+productID).hide();
        $("."+divName).fadeIn(".visible-div-"+productID);
    }
    lastProd = productID;
    lastDiv = divName;
}

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

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