I basically want to minimise some div's . Instead of using "-" and "+", I want to use some symbols (from font-awesome) to minimise and maximise the div's.
My question concerning this; how can I insert the classes of the icons in this piece of code? I tried by replacing the .html part with .attr, but that didn't worked out.
<script>
$(".btn-minimize").click(function(){
if($(this).html() == "-"){
$(this).html("+");
}
else{
$(this).html("-");
}
$(".widget-content").slideToggle();
});
</script>
Thanks a lot.
Update:
Thanks a lot for your help so far. But the sibling part doesn't really fit me because .widget-content
isn't one of the button.
So to summarise; when I want to minimise one widget and press to button, all the widgets with the class .widget-content
minimise.
This is a piece of my HTML
<!--widget-1-2-->
<section class="widget span3">
<!--front-->
<div id="front" class="widget-1-2 flip">
<div class="widget-header">
<h2>Monthly Statistics</h2>
<div class="btn-group pull-right">
<button id="btn-front" class="btn"><i class="icon-cogs icon-white" alt="settings"></i></button>
<button class="btn btn-minimize"><i class="icon-chevron-up icon-white" alt="minimize"></i></button>
<button class="btn btn-close"><i class="icon-remove icon-white" alt="close"></i></button>
</div>
</div>
<div class="widget-content"><p>Dit is de voorkant.</p></div>
</div><!--/front-->
</section>
And the JavaScript part:
<script>
$(".icon-chevron-up").click(function(){
$(this).toggleClass("icon-chevron-down");
$(".widget-content").slideToggle();
});
</script>
Say you give .btn-minimize
the minus icon in CSS. You also give .btn-minimize.btn-plus
the plus icon. Your javascript can then look like this:
$(".btn-minimize").click(function(){
$(this).toggleClass('btn-plus');
$(".widget-content").slideToggle();
});
Here's an example on JSFiddle: http://jsfiddle.net/uzmjq/
You can use CSS to apply the symbols/icons as background images, then just have a separate CSS class which contains the other icons which and then just toggle this class in the element.
CSS
.btn-minimize {
background-image: url("/path/to/icon");
}
.maximize {
background-image: url("/path/to/another/icon");
}
jQuery
$(".btn-minimize").click(function() {
$(this).toggleClass("maximize");
$(".widget-content").slideToggle();
});
来源:https://stackoverflow.com/questions/16164672/minimize-maximize-divs-with-jquery