在div宽度更改上应用bootstrap而不是窗口更改

时间:2015-07-08 06:31:37

标签: javascript jquery html css twitter-bootstrap

我有一个在我的项目中动态扩展的div。但我需要应用bootstrap列样式来扩展div。我根据窗口调整大小而不是div大小更改应用。

我的代码是:

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
  <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
  <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<script>
$(document).ready(function(){
    $("button").click(function(){
        $("div").width($("div").width()+20);
    });
});
</script>
</head>
</head>
<body>
</br>
<div class="container-fluid" style="background-color:orange;width:200px;height:200px;">

    <div class="col-sm-6" style="background-color:lavender;">
      <p>Lorem ipsum dolor sit amet</p>
    </div>
    <div class="col-sm-6" style="background-color:lavenderblush;">
      <p>Sed ut perspiciatis unde omnis </p>
    </div>

</div>


<button>expand</button>
</body>
</html>

按钮会在每次点击时继续展开div。

请帮助我们将bootstrap样式应用于动态扩展的div。

1 个答案:

答案 0 :(得分:0)

  1. 你在px中给你div内联样式宽度。
  2. parent div overflow:auto。
  3. 在jquery
  4. 中更改列宽的宽度

    &#13;
    &#13;
     <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
      <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
    
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
      <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
    <script>
    $(document).ready(function(){
    console.log($("div").width());
        $("button").click(function(){
            $("div.col-sm-6").width($("div.col-sm-6").width()+20);
        });
    });
    </script>
    
    
    
    <div class="container-fluid" style="background-color:orange;height:200px;overflow:auto;">
    
        <div class="col-sm-6 " style="background-color:lavender;">
          <p>Lorem ipsum dolor sit amet </p>
        </div>
        <div class="col-sm-6 " style="background-color:lavenderblush;">
          <p>Sed ut perspiciatis unde omnis</p>
        </div>
    
    </div>
    <button>expand</button>
    &#13;
    &#13;
    &#13;