简体   繁体   English

Javascript似乎没有取消隐藏我的元素

[英]Javascript doesn't seem to unhide my element

I have made the following code in jsfiddle, and if I check the checkbox, the div Nime should be unhidden and vice versa , but it is not happening, what am I doing wrong? 我已经在jsfiddle中编写了以下代码,如果我选中了该复选框,则div Nime应该被隐藏,反之亦然,但是这没有发生,我在做什么错?

Code in JSfiddle: JSfiddle中的代码:
Html: HTML:

<label>Newspaper?</label>
        <input type="checkbox" name="nieu" onclick="change()"><br/><br/>
    <div id="nime">
    <label>How?</label>
        <select name="nime"> 
            <option value="email">Email</option>
            <option value="post">Post</option>
            <option value="beide">Both</option>
        </select>
    </div>

Javascript: 使用Javascript:

function change(){
    which = document.getElementById('nime');

    if(which.style.display=="block"){
        which.style.display="none";
    }else{
        which.style.display="block";
    }

};

you are missing return in the if block. 您缺少if块中的返回。

function change(){
    which = document.getElementById('nime');
    if(which.style.display=="block"){
        which.style.display="none";
        return;
    }
    which.style.display="block"
}

Use the classList API with the Element.querySelector() API 将classList APIElement.querySelector()API一起使用

 function change(){ which.classList.toggle('visibility');// toggle a given class each time we click on the element woth the name nieu }; var which = document.querySelector('#nime'),// get element with ID=nime //which = document.querySelector('[id=nime]'), nieu = document.querySelector('[name=nieu]');// get element with name=nime // add the click event to nieu nieu.addEventListener("click", change, false) 
 .visibility{ /*this is our class to hide the element*/ display: none } 
 <label>Newspaper?</label> <input type="checkbox" name="nieu"><br/><br/> <div id="nime"> <label>How?</label> <select name="nime"> <option value="email">Email</option> <option value="post">Post</option> <option value="beide">Both</option> </select> </div> 

Same with the hidden state on load just add class .visibility 与加载时的隐藏状态相同,只需添加类.visibility

 function change(){ which.classList.toggle('visibility'); }; var which = document.querySelector('#nime'), nieu = document.querySelector('[name=nieu]'); nieu.addEventListener("click", change, false) 
 .visibility{ display: none } 
 <label>Newspaper?</label> <input type="checkbox" name="nieu"><br/><br/> <div id=nime class=visibility> <label>How?</label> <select name="nime"> <option value="email">Email</option> <option value="post">Post</option> <option value="beide">Both</option> </select> </div> 

Alternative to javascript 替代javascript

you can use jquery to make your life easier but JS is recommended 您可以使用jquery来简化生活,但建议使用JS

 $('[name=nieu]').click(function(){ $('#nime').toggleClass("visibility") }) 
 .visibility{ display: none } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script> <label>Newspaper?</label> <input type="checkbox" name="nieu"><br/><br/> <div id="nime"> <label>How?</label> <select name="nime"> <option value="email">Email</option> <option value="post">Post</option> <option value="beide">Both</option> </select> </div> 

to hide the element on load just add the class to it 隐藏加载时的元素,只需向其添加类

 $('[name=nieu]').click(function(){ $('#nime').toggleClass("visibility") }) 
 .visibility{ display: none } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script> <label>Newspaper?</label> <input type="checkbox" name="nieu"><br/><br/> <div id="nime" class=visibility><!-- add class visibility--> <label>How?</label> <select name="nime"> <option value="email">Email</option> <option value="post">Post</option> <option value="beide">Both</option> </select> </div> 

or if you don't want to add css 或者如果您不想添加CSS

the Jquery methode jQuery方法

 $('[name=nieu]').click(function(){ $('#nime').is(":visible") ? $('#nime').hide(): $('#nime').show() }) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script> <label>Newspaper?</label> <input type="checkbox" name="nieu"><br/><br/> <div id="nime" class=visibility><!-- add class visibility--> <label>How?</label> <select name="nime"> <option value="email">Email</option> <option value="post">Post</option> <option value="beide">Both</option> </select> </div> 

The javascript methode javascript方法

 function change(){ which.style.display == "none"? which.style.display = "block" : which.style.display = "none" }; var which = document.querySelector('[id=nime]'), nieu = document.querySelector('[name=nieu]'); nieu.addEventListener("click", change, false) 
 <label>Newspaper?</label> <input type="checkbox" name="nieu"><br/><br/> <div id="nime"> <label>How?</label> <select name="nime"> <option value="email">Email</option> <option value="post">Post</option> <option value="beide">Both</option> </select> </div> 

The content after the if statement is always going to run because the function doesn't stop just because the if criteria was true. if语句之后的内容将始终运行,因为该函数不会仅仅因为if条件为true而停止。 Also, your statement to set the value to block has the double ='s. 另外,将值设置为block的语句具有double =。 It should not. 它不应该。

if(which.style.display=="block"){
    which.style.display="none";
} else {
    which.style.display="block";
}

In your JSFiddle, please change the way you declare your function to make it global, or set the JS to No Wrap ( in head ). 在您的JSFiddle中,请更改声明函数的方式以使其成为全局函数,或将JS设置为No Wrap(在头部)。 ( See this question for more info ) 有关更多信息,请参见此问题

window.changing = function() { // }

Other notes from your initial post: you have two equal signs when you set the display to block, which doesn't set the value. 初始帖子中的其他注意事项:将显示设置为阻止时,您有两个等号,但不设置该值。 Use one. 使用一个。 Also, you need a return inside the if statement ( or an if/else ) or it will always run the rest of the code. 另外,您需要在if语句(或if / else)内返回,否则它将始终运行其余代码。

window.changing = function() {
    which = document.getElementById('nime');

    if(which.style.display=="block"){
        which.style.display="none";
        return; // otherwise the line below is always shown
    }

    which.style.display = "block"; ///// THIS LINE I EDITED
};

First of all, you need to either exit function change() when setting "display=none" or wrap "display=block" in an "else" statement, or else it will ever set "display=block" no matter what was previous value. 首先,您需要在设置“ display = none”时退出函数change()或在“ else”语句中包装“ display = block”,否则无论以前的版本是什么,它都将设置“ display = block”值。

Actually, you're currently not setting "display=block" after if() because you're using comparison operator ("==") instead of assignment operator ("="). 实际上,当前您没有在if()之后设置“ display = block”,因为您使用的是比较运算符(“ ==”)而不是赋值运算符(“ =”)。

Your function change() should be: 您的函数change()应该为:

function change(){
    which = document.getElementById('nime');

    if(which.style.display=="block") {
        which.style.display="none";
        return;
    }
    which.style.display="block";
};

or 要么

function change(){
    which = document.getElementById('nime');

    if(which.style.display=="block") {
        which.style.display="none";
    }
    else {
        which.style.display="block";
    }
};

Use the onchange event and the function as follows 使用onchange事件和函数,如下所示

function change(){
var which = document.getElementById('nime');
var display = window.getComputedStyle(which, null).getPropertyValue('display');

if(display == 'none') {
    which.style.display = 'block';
    return;
}

if (display == 'block') {
 which.style.display = 'none';
    return;
}

}

HTML HTML

<label>Newspaper?</label>
    <input type="checkbox" name="nieu" onchange="change()"><br/><br/>
<div id="nime">
<label>How?</label>
    <select name="nime"> 
        <option value="email">Email</option>
        <option value="post">Post</option>
        <option value="beide">Both</option>
    </select>
</div>

http://jsfiddle.net/pwx0gs2e/23/ http://jsfiddle.net/pwx0gs2e/23/

This is just an alternative using only css 这只是仅使用CSS的替代方法

General sibling selectors 通用兄弟选择器

 #nime{display: none} [name=nieu]:checked ~ #nime{display: block} 
 <label>Newspaper?</label> <input type="checkbox" name="nieu"><br/><br/> <div id="nime"> <label>How?</label> <select name="nime"> <option value="email">Email</option> <option value="post">Post</option> <option value="beide">Both</option> </select> </div> 

css adjacent selector CSS相邻选择器

remove <br/><br/> 删除<br/><br/>

 #nime{display: none;clear: both} [name=nieu]:checked + #nime{display: block} 
 <label>Newspaper?</label> <input type="checkbox" name="nieu"> <div id="nime"> <label>How?</label> <select name="nime"> <option value="email">Email</option> <option value="post">Post</option> <option value="beide">Both</option> </select> </div> 

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

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