单击按钮时未隐藏元素
Element not hidden when button is clicked
为什么在jsFiddle中单击按钮时display = "none"
不工作?
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:
$('input[type=button]').click( function() {
document.getElementsByClassName("hidden").style.display = "none";
});
document.getElementsByClassName()
返回类似于Array的对象HTMLCollection。您正在尝试对此数组应用HTML节点属性。
首先从这个数组中选择DOMNode,然后应用样式属性,如下所示。
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" />
或者,您可以使用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" />
在纯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" />
您可以使用jQuery轻松地完成这项工作,因为您已经将其包含在项目中,所以没有开销。只需使用hide()
从视图中删除元素:
$('input[type=button]').click(function() {
$(".hidden").hide();
});
工作示例
document.getElementsByClassName
返回一个类数组。
选择数组的第一个元素。
$('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" />
这是更新的jsFiddle
不需要混合jQuery和JavaScript,这里有一个JS方法:
document.querySelector('input').onclick = function() {
document.querySelector('.hidden').style.display = "none";
}
getElementsByClassName
不能工作的原因是它是一个数组。您需要循环遍历它和display:hide;
,或者在它后面附加[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" />
当页面上使用jQuery时,为什么要使用Native JS。
http://jsfiddle.net/ritesh14887/pUeue/3442/
$('input[type=button]').click( function() {
$(".hidden").css('display','none');
});
相关文章:
- 在单击任何位置时隐藏元素,而不检查每次DOM单击
- JavaScript-在手机上不工作的情况下,在外部单击时隐藏元素
- 随时隐藏元素
- 根据特定条件使用ng显示/ng隐藏来显示/隐藏元素
- 隐藏元素直到加载完成(ng斗篷不是我需要的)
- 在具有类的元素中隐藏元素
- javascript()onclick隐藏元素-dos'我不在firefox工作
- JQueryHide()在按钮中使用元素时不会隐藏元素
- 使用Javascript取消隐藏元素
- 如果鼠标离开父对象,则隐藏元素
- 如何在jquery ajax成功html响应中找到输入隐藏元素
- 基于事件在循环中隐藏元素
- 渐进式增强-不使用CSS隐藏元素
- 隐藏元素后更新滚动条
- jQuery没有隐藏元素
- jQuery隐藏元素在使用CSS显示时保持隐藏状态
- 如何使用javascript在区域外单击时隐藏元素
- 如何在加载网页时隐藏元素
- JS:隐藏元素上的事件侦听器
- HTML 显示/隐藏元素 JS 不起作用