JavaScript 隐藏和显示内容
我一直在寻找隐藏和显示内容的纯 CSS 答案,但运气不佳后,我一直在研究一段 JavaScript 代码。我的问题在代码下面,因为它可以帮助您先查看代码。
<script type="text/javascript">
$(document).ready(function(){
$('.text_container').addClass("visible");
$('.text_container').click(function() {
var $this = $(this);
if ($this.hasClass("visible")) {
$(this).removeClass("visible").addClass("hidden");
} else {
$(this).removeClass("hidden").addClass("visible");
}
});
});
</script>
<div id="services" class="text_container">
<h4>SERVICES</h4>
<div>
<p>Loads of text blah blah blah</p>
</div>
</div>
/* HIDE and SHOW content JavaScript function */
.hidden div {display:none;}
.visible div {display:block;}
.text_container {
background-color: #39b54a;
background-image: url("pattern2.png");
border: 1px solid #777777;
box-shadow: 0 1px 1px inset, 0 0 40px rgba(0, 0, 0, 0.5) inset, 0 16px 0 rgba(255, 255, 255, 0.4) inset, 0 4px 5px rgba(0, 0, 0, 0.6);
color: #000000;
padding: 5px;
text-align: left;
width: auto;
}
.text_container h4 {
cursor: pointer;
}
.text_container div p {
margin-bottom: 10px;
}
.visible > div {
display: block;
font-size: 17px;
height: 100%;
}
#rating > div {
clear: left;
height: 260px;
}
/* end of HIDE and SHOW content javascript function */
目前,正如预期的那样,class = text_container 区域的 div 是可单击的,因此,如果在选择表单时将表单放置在子 DIV 中,则内容会隐藏。我只想制作 H4 元素 可点击,因此点击显示的内容不会隐藏该内容。
我对 JavaScript 毫无用处,我想这需要重新调整 js。
I was searching for a pure CSS answer to hiding and showing content but after no luck I have been playing around with a piece of JavaScript code. My question is below the code, as it may help you to see the code first.
<script type="text/javascript">
$(document).ready(function(){
$('.text_container').addClass("visible");
$('.text_container').click(function() {
var $this = $(this);
if ($this.hasClass("visible")) {
$(this).removeClass("visible").addClass("hidden");
} else {
$(this).removeClass("hidden").addClass("visible");
}
});
});
</script>
<div id="services" class="text_container">
<h4>SERVICES</h4>
<div>
<p>Loads of text blah blah blah</p>
</div>
</div>
/* HIDE and SHOW content JavaScript function */
.hidden div {display:none;}
.visible div {display:block;}
.text_container {
background-color: #39b54a;
background-image: url("pattern2.png");
border: 1px solid #777777;
box-shadow: 0 1px 1px inset, 0 0 40px rgba(0, 0, 0, 0.5) inset, 0 16px 0 rgba(255, 255, 255, 0.4) inset, 0 4px 5px rgba(0, 0, 0, 0.6);
color: #000000;
padding: 5px;
text-align: left;
width: auto;
}
.text_container h4 {
cursor: pointer;
}
.text_container div p {
margin-bottom: 10px;
}
.visible > div {
display: block;
font-size: 17px;
height: 100%;
}
#rating > div {
clear: left;
height: 260px;
}
/* end of HIDE and SHOW content javascript function */
Currently as expected the div with class = text_container area is clickable, so if a place a form in the child DIV when you select the form the content hides. I Would like to make only the H4 element
clickable so clicking on the shown content will not hide the content.
I am useless at JavaScript and I imagine this requires rejigging the js.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
你可以使用:
$(' ... ') 中的内容就像一个 CSS 选择器,所以如果你了解 CSS 那么这对你来说不是问题。
使用 CSS,您可以使用以下方式设置 h4 元素的样式:
同样,您可以使用 jQuery 创建一个包装集,并通过以下方式选择它:
You can use:
The content in the $(' ... ') is just like a CSS selector, so if you know CSS then it won't be a problem for you.
With CSS you could style that h4 element with:
and just the same, you can create a wrapped set with jQuery that selects it with:
这将实现您的目标,示例:
我们选择 H4 并添加点击事件,然后使用 .parent() 访问到父级 DIV。
This will accomplish your goal, example:
We are selecting the H4 and adding the click event to it, but then using .parent() to access to parent DIV.
将
$('.text_container')...
替换为$('.text_container h4')...
或者更好的是$('h4' )....
如果它是页面上唯一的 H4 元素。replace
$('.text_container')...
with$('.text_container h4')...
or, better yet,$('h4')....
if it's the only H4 element on the page.将此:更改
为:
change this:
to this:
只需替换为
Simply replace with
我不确定某人如何能够点击带有
display: none
的元素。另外,将其替换为:
完整代码:
I am not sure how someone will be able to click on an element with
display: none
. Also, replace this:With this:
Full code: