简体   繁体   English

最小化div最大化? 适用于所有div的javascript或jquery

[英]minimize maximize the div ? javascript or jquery which works for all the div

i want to give minimize and maximize facility to my div..for that i have written following code in my jsp page 我想给我的div提供最小化和最大化的设施..因为我已经在我的jsp页面中编写了以下代码

<div id="widnow" style="width: auto;">
    <div id="title_bar">
      <button type="button" class="max" id="button">
    <img id="max" src="<%=request.getContextPath()%>/img/Minimize.png" />
</button>
    </div>
      <div id="box" style="height:auto; width: auto;">
       <span id="info3" style="display: none;"></span>

    <div id="chart3" style="width:80%; height:300px;"></div>



    </div>

 </div>

following is my css 以下是我的CSS

#widnow{
    width:400px;
    border: 1px solid #DCDCDC;

}



#button{
    width: 25px;
    height: 24px;
    float:right;
    cursor:pointer;
    position:relative;
    margin: 0px 0px 0px 0px;

}
#title_bar{
    background-image: url("<%=request.getContextPath()%>/img/Header Background.png");
    height: 25px;
    width: auto;
  border-bottom: 1px solid #DCDCDC;

}

following is my js 以下是我的js

$("#button").click(function(){

    var isclass = $(this).attr('class');
    if (isclass == "max") 

    {
        $("#max").attr("src","<%=request.getContextPath()%>/img/Minimize.png");
        $(this).removeClass('max');
    }
    else{
        $(this).addClass('max');
        $("#max").attr("src","<%=request.getContextPath()%>/img/Maximize.png");
    }
    $("#box").slideToggle();

});

Its Working Well When there is one div i want to give this facility..but with one single java script function or with j-query how can i achieve this?i have many div in single page and all should have there individual maximize and minimize facility.so can anyone tell me how can change my java script that works with all div? 它运作良好当有一个div我想给这个设施..但是有一个单独的java脚本函数或j-query我怎么能实现这个?我在单页中有很多div,所有应该有个别最大化和最小化facility.so任何人都可以告诉我如何更改我的java脚本适用于所有div?

Reconstructing your code a bit: 稍微重建你的代码:

function activateMaxMin(elemButton,elemMax,elemBox)
{

  var isclass = elemButton.attr('class');
    if (isclass == "max") 

    {
        elemMax.attr("src","<%=request.getContextPath()%>/img/Minimize.png");
        elemButton.removeClass('max');
    }
    else{
        elemButton.addClass('max');
        elemMax.attr("src","<%=request.getContextPath()%>/img/Maximize.png");
    }
    elemBox.slideToggle();

}

$("#button").click(function(){
activateMaxMin($(this),$("#max"),$("#box"));

});

In case there is another button like this, use: 如果有这样的另一个按钮,请使用:

$("#button1").click(function(){
activateMaxMin($(this),$("#max1"),$("#box1"));

});

Of course, it could have been done in more efficient way, but that would need rewriting your code entirely. 当然,它可以以更有效的方式完成,但这需要完全重写代码。

Just change id notation "#" to class selector "." 只需将id notation "#"更改为类选择器"." like #max to .max 比如#max.max

What browser does it would work only for first occurrence of the id and try to change the img id to class 什么浏览器只会在第一次出现id起作用,并尝试将img id更改为class

Well in your case that is different i have created a fiddle see at that if that helps you: 那么在你的情况下,我已经创建了一个小提琴,如果这有助于你:

http://jsfiddle.net/32e2V/ http://jsfiddle.net/32e2V/

$(".button").click(function () {
  $(this).closest('div').siblings(".box").slideToggle();
  if ($(this).text() == "max") {
    $(this).text('min');
  } else {
    $(this).text('max');
  }
});
<div id="widnow" class='window' style="width: auto;">
    <div id="title_bar" class='title_bar'>
      <button type="button" class="maxMinButton" id="button">
    <img id="max" src="<%=request.getContextPath()%>/img/Minimize.png"  class='max'/>
</button>
    </div>
      <div id="box" style="height:auto; width: auto;" class='box'>
       <span id="info3" style="display: none;"></span>

    <div id="chart3" style="width:80%; height:300px;"></div>



    </div>

 </div>

//javascript function // javascript函数

function activateMaxMin(elemButton,elemMax,elemBox) { function activateMaxMin(elemButton,elemMax,elemBox){

var isclass = elemButton.attr('class'); var isclass = elemButton.attr('class'); if (isclass == "max") if(isclass ==“max”)

{
    elemMax.attr("src","<%=request.getContextPath()%>/img/Minimize.png");
    elemButton.removeClass('max');
}
else{
    elemButton.addClass('max');
    elemMax.attr("src","<%=request.getContextPath()%>/img/Maximize.png");
}
elemBox.slideToggle();

} }

$(document).ready(function(){
$(".maxMinButton").click(function(){

var elemButton=$(this);
var maxMinWidnow=button.closes('.window');
var elemMax=$(".max:firs",maxMinWidnow);
var elemBox=$(".box:first",maxMinWidnow);

activateMaxMin(elemButton,elemMax,elemBox)
});
});

This is the best I could get without doing lots of modifications to your code. 如果不对代码进行大量修改,这是我能得到的最好的。 Notice the added classes in each div in html, that are necessary. 注意html中每个div中添加的类是必要的。 Be careful during modification. 修改时要小心。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM