简体   繁体   English

jQuery宽度得到错误的值

[英]jQuery width getting wrong value

I have a problem with the jquery method .width(). 我有一个jquery方法.width()的问题。 I have 4 different textarea that need to resize according to the size of a div that contains them, so I call the method .width() to get the actual width of the div (wich anyway is 100% of the window) but this method always get the wrong value even after wrapping everything in a $(window).ready(). 我有4个不同的textarea需要根据包含它们的div的大小调整大小,所以我调用方法.width()来获取div的实际宽度(无论如何是窗口的100%)但是这个方法即使在$(窗口).ready()中包装所有内容后,也总是得到错误的值。

Here is the CSS 这是CSS

#contCode{
    width: 100%;
    height: 500px;
    margin-top: 50px;
    float: left;
    padding: 0;
}
.codeArea{
    float: left;
    font-family: "Lucida Console", Monaco, monospace;
    height:100%;
    padding: 5px 0 0 5px;
    margin: 0;
    border-right: 1px solid black;
    border-bottom: 1px solid black;
    resize: none;
}

This is the HTML 这是HTML

<div id="contCode">
    <textarea class="codeArea" placeholder="Put your code here"></textarea>
    <textarea class="codeArea" placeholder="Put your code here"></textarea>
    <textarea class="codeArea" placeholder="Put your code here"></textarea>
    <div class="codeArea"></div>
</div>

And this is the script 这是剧本

$(window).ready(function (){
        var wi = $("#contCode").width(); 
        var wiTex;
        wiTex = (wi) / 4;
        $(".codeArea").width(wiTex);
    });

I have already tried with outerWidth and innerWith and got the same result. 我已经尝试使用outerWidth和innerWith并得到了相同的结果。

I found two issues. 我发现了两个问题。 The first I knew about 我知道的第一个

  1. You need to add box-sizing: border-box so that when you set the width of the boxes the border width is included. 您需要添加box-sizing: border-box ,这样当你设置的width包含边框宽度的箱子。 If you don't, a box set to 100px with a 1px border will actually be 102px wide. 如果不这样做,设置为100px且边框为1px框实际上102px宽。

  2. The second issue I found was with jQuery's .width() method. 我发现的第二个问题是使用jQuery的.width()方法。 For some reason even when I told it manually to set to .width('100px') the boxes were being set to 112px . 出于某种原因,即使我手动设置为.width('100px') ,盒子也被设置为112px I changed it to use vanilla JavaScript instead. 我改为使用vanilla JavaScript。

You can see from the demo below that the boxes align now. 您可以从下面的演示中看到这些框现在对齐。

 $(window).ready(function() { var wi = $("#contCode").width(); var wiTex = (wi) / 4; document.querySelectorAll('.codeArea').forEach(function(node) { node.style.width = wiTex+'px' }) }); 
 #contCode { width: 100%; height: 500px; margin-top: 50px; float: left; padding: 0; } .codeArea { float: left; font-family: "Lucida Console", Monaco, monospace; height: 100%; padding: 5px 0 0 5px; margin: 0; border-right: 1px solid black; border-bottom: 1px solid black; resize: none; background: #ccc; box-sizing: border-box; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="contCode"> <textarea class="codeArea" placeholder="Put your code here"></textarea> <textarea class="codeArea" placeholder="Put your code here"></textarea> <textarea class="codeArea" placeholder="Put your code here"></textarea> <div class="codeArea">The result area</div> </div> 

just use this: 只是用这个:

$(document).ready(function (){
    var wi = $("#contCode").width(); 
    var wiTex;
    wiTex = (wi) / 4;
    $(".codeArea").width(wiTex - 7); //here is the trick =)
});

why i substruct 7 pixels? 为什么我下调7像素? That's because of how css calculate width of an element. 这是因为css如何计算元素的宽度。

In css, width equals width + padding + border . 在css中,width等于width + padding + border In your case, real width of element would be: wiTex + 5px of left padding + 2 pixels of borders 在您的情况下,元素的实际宽度将是: wiTex + 5px of left padding + 2 pixels of borders


you can even remove javascript code and use this css : 你甚至可以删除javascript代码并使用此css

.codeArea{
    float: left;
    font-family: "Lucida Console", Monaco, monospace;
    height:100%;
    padding: 5px 0 0 5px;
    margin: 0;
    border-right: 1px solid black;
    border-bottom: 1px solid black;
    resize: none;
    width: calc(25% - 7px); /* magic =) */
}

You can see it for yourself. 你可以自己看看

Try this! 试试这个!

  #contCode{ width: 100%; height: 500px; margin-top: 50px; float:left; padding: 0; } .codeArea{ float: left; font-family: "Lucida Console", Monaco, monospace; height:100%; width:22%; //Added this padding:5px 0 0 5px; margin: 0; border:1px solid #ccc; color:#333; resize: none; background-color:#eee; } 
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="contCode"> <textarea class="codeArea" placeholder="Put your code here"></textarea> <textarea class="codeArea" placeholder="Put your code here"></textarea> <textarea class="codeArea" placeholder="Put your code here"></textarea> <div class="codeArea">Hello World</div> </div> 

Cheers! 干杯!

There is a line break after the 3rd textarea. 在第3个textarea之后有一个换行符。 Is it because of the CSS3 box-sizing Property? 是因为CSS3盒子大小的属性? By default it is content-box,which only contains the content itself but not the padding. 默认情况下,它是content-box,它只包含内容本身但不包含填充。 You need to change it to border-box; 你需要将它改为边框;

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

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