简体   繁体   English

动态隐藏和显示div

[英]dynamically hiding and showing divs

How can I achieve this dynamically using JavaScript? 如何使用JavaScript动态地实现这一目标?

onselect radio button 1: Shows div 1,2,5, hides (if not already hidden) divs 3,4,6,7 onselect单选按钮1:显示div 1,2,5,隐藏(如果尚未隐藏)div 3,4,6,7

onselect radio button 2: Shows div 3,4,6,7, hides (if not already hidden) divs 1,2,5 onselect单选按钮2:显示div 3、4、6、7,隐藏(如果尚未隐藏)div 1,2,5

<input type="radio" id="button-1" name="button" />Button 1
<input type="radio" id="button-2" name="button" />Button 2

<div id="div-1"></div>
<div id="div-2"></div>
<div id="div-3"></div>
<div id="div-4"></div>
<div id="div-5"></div>
<div id="div-6"></div>
<div id="div-7"></div>

edit I formulated my question poorly, will formulate it better when at home after work.. 编辑我提出的问题很差,下班后在家时会更好地提出。

To make it easier on yourself, add a class to the two groups of radio buttons, something like divGroup1, divGroup2 为了使自己更轻松,请将一个类添加到两组单选按钮中,例如divGroup1,divGroup2

    <div class="divGroup1" id="div-1"></div>
    <div class="divGroup1" id="div-2"></div>
    <div class="divGroup2" id="div-3"></div>
    <div class="divGroup2" id="div-4"></div>
    <div class="divGroup1" id="div-5"></div>
    <div class="divGroup2" id="div-6"></div>
    <div class="divGroup2" id="div-7"></div>

then in jQuery, do something like this: 然后在jQuery中,执行以下操作:

$("#button-1").click(function()
{
    $(".divGroup1").show();
    $(".divGroup2").hide();
});

$("#button-2").click(function()
{
    $(".divGroup2").show();
    $(".divGroup1").hide();
});
the solution is in  jQuery

<input type="radio" id="button-1" name="button" />Button 1
    <input type="radio" id="button-2" name="button" />Button 2

    <div class="c1" id="div-1"></div>
    <div class="c2" id="div-2"></div>
    <div class="c1" id="div-3"></div>
    <div class="c2" id="div-4"></div>
    <div class="c1" id="div-5"></div>
    <div class="c2" id="div-6"></div>
    <div class="c2" id="div-7"></div>

    $('#button-1').click(function(){
    $('.c1').show();
    $('.c2').hide();
    })

    $('#button-2').click(function(){
    $('.c2').show();
    $('.c1').hide();
    })

Try to use the following parts from jquery 尝试使用jquery中的以下部分

$('#button-1').click(function(){.... your code here .... });
$('#button-2').click(function(){.... your code here .... });

and

$('#div-1').show();

and

$('#div-2').hide();

and when you put it all together it will all work. 当你把它们放在一起的时候,一切都会起作用。

JQuery - click methods. jQuery-单击方法。 Hide the relevant divs in the function, which will hide them whatever their state. 在函数中隐藏相关的div,这将隐藏它们的任何状态。

Try this: 尝试这个:

$('#button-1').select(function() {
    $('#div-1, #div-2, #div-5').show();
    $('#div-3, #div-4, #div-6, #div-7').hide();
});

$('#button-2').select(function() {
    $('#div-1, #div-2, #div-5').hide();
    $('#div-3, #div-4, #div-6, #div-7').show();
});

JSFiddle seems to be slow but I think this should work: JSFiddle似乎很慢,但是我认为这应该起作用:

http://jsfiddle.net/nfypC/4/ http://jsfiddle.net/nfypC/4/

$('#div-1').hide();
$('#div-2').hide();
$('#div-3').hide();
$('#div-4').hide();
$('#div-5').hide();
$('#div-6').hide();
$('#div-7').hide();

$("#button-1").click(function () {
    $('#div-1').show();
    $('#div-2').show();
    $('#div-5').show();

    $('#div-3').hide();
    $('#div-4').hide();
    $('#div-6').hide();
    $('#div-7').hide();

});

$("#button-2").click(function () {
    $('#div-1').hide();
    $('#div-2').hide();
    $('#div-5').hide();

    $('#div-3').show();
    $('#div-4').show();
    $('#div-6').show();
    $('#div-7').show();

});

This is relatively basic to do. 这是相对基本的操作。 As others are suggesting, i would suggest using a third party javascript library like jQuery or PrototypeJs. 正如其他人建议的那样,我建议使用第三方javascript库,例如jQuery或PrototypeJs。

If you choose jQuery, the following pages should help you out: http://api.jquery.com/show/ , http://api.jquery.com/hide/ and http://docs.jquery.com/Main_Page 如果您选择jQuery,则以下页面将为您提供帮助: http : //api.jquery.com/show/,http : //api.jquery.com/hide/http://docs.jquery.com/Main_Page

If you choose PrototypeJS: http://www.prototypejs.org/api/element/show , http://www.prototypejs.org/api/element/hide and http://www.prototypejs.org/learn . 如果您选择PrototypeJS: http : //www.prototypejs.org/api/element/show,http : //www.prototypejs.org/api/element/hidehttp://www.prototypejs.org/learn

In future, I would recommend you visit these resources first as most of the answers here are making use of libraries like this. 将来,我建议您先访问这些资源,因为此处的大多数答案都利用了这样的库。

Also, these libraries do ALOT. 而且,这些库很多。 Really worth getting to know them! 真正值得认识他们!

First, what do you mean by dynamically? 首先,动态意味着什么? You'll have to code the relationship somewhere. 您必须在某处编码该关系。

What you add a custom attribute to your divs like: 您向div添加自定义属性的方式如下:

<input type="radio" id="button-1" name="button" />Button 1 
<input type="radio" id="button-2" name="button" />Button 2  

<div id="div-1" linktoButton="button-1"></div> 
<div id="div-2" linktoButton="button-1"></div> 
<div id="div-3" linktoButton="button-2"></div> 
<div id="div-4" linktoButton="button-2"></div> 
<div id="div-5" linktoButton="button-1"></div> 
<div id="div-6" linktoButton="button-2"></div> 
<div id="div-7" linktoButton="button-2"></div> 

Then you could have a single onclick event that would take the id of the button you click, show all divs where linktoButton="button-1' and hide the rest. 然后,您可能会有一个onclick事件,该事件将获取您单击的按钮的ID,显示所有div,其中linktoButton =“ button-1'并隐藏其余部分。

You can use the "Has Attribute" selector in jQuery http://api.jquery.com/has-attribute-selector/ 您可以在jQuery http://api.jquery.com/has-attribute-selector/中使用“具有属性”选择器

Just remember that this way will have to walk through every element in the dom and may be slow. 请记住,这种方式将必须遍历dom中的每个元素,并且可能很慢。 You may want to have a container div that you can get by ID and just use the attribute selector within the children. 您可能希望有一个容器div,可以通过ID获得它,而只在子级中使用属性选择器。

Hope this points you in the right direction, and remember, there's 100 ways to skin this cat. 希望这能为您指明正确的方向,并记住,有100种方法可以给这只猫剥皮。

<input type="radio" onclick="hideDivOneTwoAndFiveAndShowThreeFourSixAndSeven()" id="button-1" name="button" />Button 1
<input type="radio" onclick="showDivOneTwoAndFiveAndHideThreeFourSixAndSeven()" id="button-2" name="button" />Button 2

function hideDivOneTwoAndFiveAndShowThreeFourSixAndSeven(){
   $("#div-1,#div-2,#div-5").hide();
   $("#div-3,#div-4,#div-5,#div-7").show();
}

function showDivOneTwoAndFiveAndHideThreeFourSixAndSeven(){
   $("#div-1,#div-2,#div-5").show();
   $("#div-3,#div-4,#div-5,#div-7").hide();
}

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

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