[英](jQuery) Toggle div style “display:none” to “display:inline”
I have 2 divs which and I want to be able to toggle between them onClick of a button (currently using .toggle();) 我有2个div,我希望能够在它们之间切换onClick按钮(当前使用.toggle();)
The div that shows on the page is div1. 页面上显示的div是div1。 This div has the style 'display:inline'.
这个div的风格是'display:inline'。 My other div (div2) starts with the style 'display:none'.
我的另一个div(div2)以“display:none”样式开头。
When the div1 switches to div2, I want div2 to have the style of "display:inline". 当div1切换到div2时,我希望div2具有“display:inline”的样式。 How do I do this?
我该怎么做呢?
EDIT: This is working: 编辑:这是工作:
$(function(){
$('#button').click(function(){
$('#div1').toggleClass('hide');
if ($('#div2').is('.hidden')) {
$('#div2').removeClass('hidden');
$('#div2').addClass('show');
}
else{
$('#div2').addClass('hidden');
$('#div2').removeClass('show');
}
});
});
I would use .toggleClass()
as toggle switches between display: inline;
我会使用
.toggleClass()
作为切换display: inline;
and display: block;
和
display: block;
Create a hidden and inline class and just toggle those. 创建一个隐藏的内联类,然后切换它们。
Using plain JavaScript, you could use: 使用纯JavaScript,您可以使用:
document.getElementById('div1').style.display = 'none';
document.getElementById('div2').style.display = 'inline';
Here is a simple way to do it: 这是一个简单的方法:
For the html, we have a simple button to call the "toggleFunction" that will add and remove display classes to our Div elements as necessary. 对于html,我们有一个简单的按钮来调用“toggleFunction”,它将根据需要为Div元素添加和删除显示类。
<button onclick="toggleFunction()" >Click to toggle</button>
<div id="div1" class=" " > Now showing "Div 1" </div>
<div id="div2" class=" " > Now showing "Div 2" </div>
We'll set the default display properties of Div 1 and Div 2 to "inline" and "none" respectively, so that by default: 我们将Div 1和Div 2的默认显示属性分别设置为“inline”和“none”,因此默认情况下:
Here is the css for that: 这是css:
#div1 {
display: inline;
color:blue;
}
#div2 {
display: none;
color:red;
}
.display-none {
display: none !important;
}
.display-inline {
display: inline !important;
}
Here is the Jquery for that: 这是Jquery:
function toggleFunction() {
$("#div1").toggleClass("display-none");
$("#div2").toggleClass("display-inline");
}
You can try it out on codepen here: http://codepen.io/anon/pen/vEbXwG 你可以在codepen上试试看: http ://codepen.io/anon/pen/vEbXwG
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.