我是 JQuery 新手。有两个 HTML 元素(P 和 input)。单击按钮后,我想隐藏这两个元素。两者具有相同的类名。 简单的代码不起作用。 有什么建议请。
非常感谢 内斯
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Demo</title>
</head>
<body> <a href="http://jquery.com/">jQuery</a>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
$(document).ready(function() {
$("#hide#").click(function() {
$(.test).hide();
});
$("#show").click(function() {
$(.test).show();
});
});
</script>
<p class="test">Hello this is P element that I am trying to hide using its class</p> First name:
<input class="test1" type="text" name="fname">
<button id="hide">Hide</button>
<button id="show">Show</button>
</body>
</html>
最佳答案
$(document).ready(function(){
$('#hide').on('click', function(){
$('.test').hide()
});
$('#show').on('click', function(){
$('.test').show()
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<p class='test'>some text1</p>
<p class='test'>some text2</p>
<button type='button' id='hide'>hide</button>
<button type='button' id='show'>show</button>
关于jquery - 隐藏所有具有类名的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29374014/