简体   繁体   English

在单击“提交”时显示和隐藏具有相同类但ID不同的多个DIV

[英]Show and hide multiple DIVs with same class, but different IDs on clicking submit

I have a number of DIVs with the same class applied, but unique IDs. 我有许多具有相同类但应用了唯一ID的DIV。 I want all the DIVs to be hidden on page load, then when the submit button is clicked I want one of the DIVs (determined by the ID) to become visible. 我希望所有DIV在页面加载时都被隐藏,然后单击“提交”按钮时,我希望其中一个DIV(由ID确定)变得可见。

I thought the best way to do this would be using CSS classes. 我认为最好的方法是使用CSS类。

I have set their default class in CSS to "inactive" with .configImageTitleInactive {display:none;} 我已使用.configImageTitleInactive {display:none;} CSS中的默认类设置为“非活动”

I have a second class "active" with .configImageTitleActive {display:block;} 我使用.configImageTitleActive {display:block;}有第二个“活动”类

I have already defined and successfully populated the global variables temp1, temp2 and temp3, as these are being used elsewhere on the page. 我已经定义并成功填充了全局变量temp1,temp2和temp3,因为它们正在页面上的其他位置使用。

When the submit button is clicked I need the code to: 单击提交按钮后,我需要代码以:

  1. Check if any of the DIVs already have the configImageTitleActive applied to the class and if so, change that class back to configImageTitleInactive . 检查是否有任何DIV已经将configImageTitleActive应用于该类,如果是,则将该类更改回configImageTitleInactive

  2. Collate the 3 temp variables together (not numerically as these variables contain letters) 将3个临时变量整理在一起(由于它们包含字母,因此不进行数字排序)

  3. Compare the combined value against the IDs of the DIVs and switch on the visibilty of that DIV 将组合值与DIV的ID进行比较,然后打开该DIV的可见性

The javascript code is below: javascript代码如下:

    $(document).ready(function() {
$('img.submit').click(function() {
var $configImageTitle = temp1 + temp2 + temp3;
if ($configImageTitle == "cp-xos"){
        $("div.configImageTitleActive").removeClass("configImageTitleActive").addClass("configImageTitleInactive");
        var configImageTitleTemp = document.getElementById('cp-xos');
        $configImageTitleTemp.addClass("configImageTitleActive");
}
else if ($configImageTitle == "cp-uos"){
        $("div.configImageTitleActive").removeClass("configImageTitleActive").addClass("configImageTitleInactive");
        var configImageTitleTemp = document.getElementById('cp-uos');
        $configImageTitleTemp.addClass("configImageTitleActive");
}
else if ($configImageTitle == "cp-uod"){
        $("div.configImageTitleActive").removeClass("configImageTitleActive").addClass("configImageTitleInactive");
        var configImageTitleTemp = document.getElementById('cp-uod');
        $configImageTitleTemp.addClass("configImageTitleActive");
}
else {
        $("div.configImageTitleActive").removeClass("configImageTitleActive").addClass("configImageTitleInactive");
        var configImageTitleTemp = document.getElementById('cp-xod');
        $configImageTitleTemp.addClass("configImageTitleActive");
};
});
});

And the HTML: 和HTML:

<div class="configImageTitleBlock">
<div class="configImageTitleInactive" id="cp-xos">I am 1 CP-XOS</div>
<div class="configImageTitleInactive" id="cp-uos">I am 2 CP-UOS</div>
<div class="configImageTitleInactive" id="cp-uod">I am 3 CP-UOD</div>
<div class="configImageTitleInactive" id="cp-xod">I am 4 CP-XOD</div>

</div>

I'm open to suggestions, if there is a better way to do this. 如果有更好的方法,我愿意接受建议。 Thank you in advance for your help. 预先感谢您的帮助。

Hm, I see you are using jQuery but not using the full power of it. 嗯,我看到您使用的是jQuery,但没有使用它的全部功能。 Here's how you can do your task: 这是您可以完成任务的方式:

var configImageTitle = temp1 + temp2 + temp3;
$('div.configImageTitleActive').removeClass("configImageTitleActive").addClass("configImageTitleInactive");
$('#'+configImageTitle).addClass("configImageTitleActive");

ADDED: 添加:

As about doing it better: 至于做得更好:

<div class="configImageTitleBlock">
    <div class="block" id="cp-xos">I am 1 CP-XOS</div>
    <div class="block" id="cp-uos">I am 2 CP-UOS</div>
    <div class="block" id="cp-uod">I am 3 CP-UOD</div>
    <div class="block" id="cp-xod">I am 4 CP-XOD</div>
</div>

------- CSS --------
.configImageTitleBlock div.block {
    display: none;
}

.configImageTitleBlock div.block.active {
    display: block;
}

------- CSS --------
...eventandstuff(function() {
    var activeElem = temp1 + temp2 + temp3;
    $('.configImageTitleBlock div.block.active').removeClass("active");
    $('#'+activeElem).addClass("active");
});

CSS has priorities of how properties are applied. CSS具有如何应用属性的优先级。 In essence - the more detailed definition is in CSS - the higher priority it has. 本质上-CSS中定义越详细-优先级越高。 For example div {display: none} has lower priority than div.someclass {display: block} . 例如, div {display: none}优先级低于div.someclass {display: block}

So in essence, I defined a class active , and a CSS rule for it .block.active which is more specific than just .block , that's why when it's applied, the rule from .block.active gets higher priority. 因此,从本质上讲,我定义了一个active类,以及一个针对.block.active的CSS规则,该规则比.block更为具体,这就是为什么在应用它时, .block.active的规则将具有更高的优先级。

This approach is better because you don't need to define two separate classes for active and inactive. 这种方法更好,因为您不需要为活动和非活动定义两个单独的类。 You can only use the active one. 您只能使用active

Another advice: don't make classes that are context-specific, like configImageTitleInactive . 另一个建议:不要创建特定于上下文的类,例如configImageTitleInactive Better make a generic class inactive or active and then just write more specific rule, like .configImageTitleBlock .active - this makes it more understandable and code is cleaner. 最好使一个通用类inactiveactive ,然后编写更具体的规则,例如.configImageTitleBlock .active这使它更易于理解并且代码更.configImageTitleBlock .active

I think it would be better not to remove the main class and simply add class "active" instead, then check if element has class "active" 我认为最好不要删除主类,而只需添加“活动”类,然后检查元素是否具有“活动”类

CSS: CSS:

<style>
.configImageTitle { display: none; }
.configImageTitle.active { display: block; }
</style>

JQuery: jQuery的:

$(document).ready(function() {
    $('img.submit').click(function() {
        var $configImageTitle = temp1 + temp2 + temp3;
        if ($configImageTitle == "cp-xos"){
            $("div.configImageTitle").hasClass("active") ? $("div.configImageTitle").removeClass("active") : "";
            var configImageTitleTemp = $("#cp-xos");
                $configImageTitleTemp.addClass("active");
        } else {
            /* and so on */
        }
    });
});

You could use a better logic like try setting all the divs visibility:hidden like shown below 您可以使用更好的逻辑,例如尝试设置所有div的可见性:隐藏,如下所示

<div class="configImageTitleInactive" id="cp-xos" style="visibility:hidden">I am 1 CP-XOS</div>

and from javascript access the div and set its visibilty to visible. 并从javascript访问div并将其可见性设置为visible。 Thanks AB 谢谢AB

I guess pretty much what @Max wrote, just with a running example: 我想@Max写的几乎是一个运行示例:

http://jsbin.com/agawov/11/edit http://jsbin.com/agawov/11/edit

$( document ).ready(function() {    

 $(document)
  .find('.configImageTitleBlock > div.configImageTitleActive')
  .removeClass("configImageTitleActive");

 $(document)
  .find('.configImageTitleBlock > div#cp-xos')        
  .removeClass('configImageTitleInactive')      
  .addClass('configImageTitleActive');

});

Cheers. 干杯。

Vitor. Vitor。

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

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