简体   繁体   English

单击按钮时元素未隐藏

[英]Element not hidden when button is clicked

Why display = "none" not works when button is clicked in jsFiddle ? 为什么在jsFiddle中单击按钮时display = "none" 不起作用

HTML: HTML:

<div>This is a visible heading<div>
<div class="hidden">This is a hidden heading</div>
<div>Notice that the hidden heading still takes up space.</div>

<hr/>

JavaScript: JavaScript:

$('input[type=button]').click( function() {
 document.getElementsByClassName("hidden").style.display = "none";
});

document.getElementsByClassName() returns HTMLCollection which is an Array like object. document.getElementsByClassName()返回HTMLCollection ,它是一个类似于Array的对象。 You are trying to apply HTML Node properties on this array. 您正在尝试在此数组上应用HTML节点属性。

First select DOM Node from this array then apply style properties as shown below. 首先从此数组中选择DOM节点,然后应用样式属性,如下所示。

document.getElementsByClassName("hidden")[0].style.display = "none";

 $('input[type=button]').click( function() { document.getElementsByClassName("hidden")[0].style.display = "none"; }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div>This is a visible heading<div> <div class="hidden">This is a hidden heading</div> <div>Notice that the hidden heading still takes up space.</div> <hr/> <input type="button" value="test" /> 

Alternatively you can use jQuery to hide element. 或者,您可以使用jQuery隐藏元素。

 $('input[type=button]').click(function() { $(".hidden").first().hide(); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div>This is a visible heading<div> <div class="hidden">This is a hidden heading</div> <div>Notice that the hidden heading still takes up space.</div> <hr/> <input type="button" value="test" /> 

In Pure JavaScript you can do it as follows: 在Pure JavaScript中,您可以按照以下步骤进行操作:

 var button = document.getElementsByClassName('button')[0]; button.addEventListener('click', function() { document.getElementsByClassName("hidden")[0].style.display = "none"; }, false); 
 <div>This is a visible heading<div> <div class="hidden">This is a hidden heading</div> <div>Notice that the hidden heading still takes up space.</div> <hr/> <input class="button" type="button" value="test" /> 

You could use jQuery to do it easily, you already had included it to your project, so it is no overhead. 您可以使用jQuery轻松做到这一点,因为您已经将其包含在项目中,因此没有任何开销。 Simply use hide() to remove the element from view: 只需使用hide()从视图中删除该元素:

$('input[type=button]').click(function() {
    $(".hidden").hide();
});

Working example. 工作示例。

The document.getElementsByClassName returns an array of classes. document.getElementsByClassName返回一个类数组。

chose the first element of the array. 选择了数组的第一个元素。

 $('input[type=button]').click( function() { document.getElementsByClassName("hidden")[0].style.display = "none"; }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div>This is a visible heading<div> <div class="hidden">This is a hidden heading</div> <div>Notice that the hidden heading still takes up space.</div> <hr/> <input type="button" id="test" value="test" /> 

Here is the updated jsFiddle 这是更新的jsFiddle

No need to mix jQuery and JavaScript, here is a JS method: 无需混合使用jQuery和JavaScript,这是一种JS方法:

document.querySelector('input').onclick = function() {
  document.querySelector('.hidden').style.display = "none";
}

The reason your getElementsByClassName wont work is because it's an array. 您的getElementsByClassName无法使用的原因是因为它是一个数组。 You either need to loop through it and display:hide; 您要么需要遍历它并display:hide; all of them, or get a specific one by appending [n] after it ( n being the number of the element you want in the array, starting at 0 ). 所有这些,或在其后附加[n]获得特定的数字( n是您要在数组中的元素的编号,从0开始)。

 document.querySelector('input').onclick = function() { document.querySelector('.hidden').style.display = "none"; } 
 <div>This is a visible heading<div> <div class="hidden">This is a hidden heading</div> <div>Notice that the hidden heading still takes up space.</div> <hr/> <input type="button" value="test" /> 

Why using Native JS when you have jQuery used on page. 在页面上使用jQuery时为什么要使用Native JS。

http://jsfiddle.net/ritesh14887/pUeue/3442/ http://jsfiddle.net/ritesh14887/pUeue/3442/

$('input[type=button]').click( function() {
 $(".hidden").css('display','none');
});

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

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