Programing

jQuery에서 width, innerWidth 및 outerWidth, height, innerHeight 및 outerHeight의 차이점은 무엇입니까?

crosscheck 2020. 7. 30. 09:53
반응형

jQuery에서 width, innerWidth 및 outerWidth, height, innerHeight 및 outerHeight의 차이점은 무엇입니까?


차이점을 확인하기 위해 몇 가지 예를 작성했지만 너비와 높이에 대해 동일한 결과를 표시합니다.

<html>
    <head>
        <script type="text/javascript" src="jquery.js"></script>
        <script type="text/javascript">
            $(document).ready(function(){
                var div = $('.test');
                var width = div.width(); // 200 px
                var innerWidth = div.innerWidth(); // 200px
                var outerWidth = div.outerWidth(); // 200px

                var height = div.height(); // 150 px
                var innerHeight = div.innerHeight(); // 150 px
                var outerHeight = div.outerHeight(); // 150 px
            });

        </script>
        <style type="text/css">
            .test
            {
                width: 200px;
                height: 150px;
                background: black;
            }
        </style>
    </head>
    <body>
        <div class="test"></div>
    </body>
</html>

이 예에서는 동일한 결과가 출력되는 것을 볼 수 있습니다. 누구든지 차이점이 무엇인지 알고 있다면 적절한 대답을 보여주십시오.

감사.


이 예들을 보았습니까? 질문과 비슷해 보입니다.

너비와 높이 작업

여기에 이미지 설명을 입력하십시오

jQuery-측정 기준

jQuery : 높이, 너비, 내부 및 외부


의견에서 언급했듯이 , 설명서 에는 차이점이 무엇인지 정확하게 알려줍니다. 그러나 요약하면 :

  • innerWidth / innerHeight-패딩은 포함하지만 테두리는 포함하지 않음
  • outerWidth / outerHeight-패딩, 테두리 및 선택적으로 여백 포함
  • 높이 / 너비-요소 높이 (패딩 없음, 여백 없음, 테두리 없음)

  • 너비 = 너비를 얻습니다.

  • innerWidth = get width + padding,

  • outerWidth = get width + padding + border and optionally the margin

If you want to test add some padding, margins, borders to your .test classes and try again.

Also read up in the jQuery docs... Everything you need is pretty much there


It seems necessary to tell about the values assignations and compare about the meaning of "width" parameter in jq : (assume that new_value is defined in px unit)

jqElement.css('width',new_value);
jqElement.css({'width: <new_value>;'});
getElementById('element').style.width= new_value;

The three instructions doesn't give the same effect: because the first jquery instruction defines the innerwidth of the element and not the "width". This is tricky.

To get the same effect you must calculate the paddings before (assume var is pads), the right instruction for jquery to obtain the same result as pure js (or css parameter 'width') is :

jqElement.css('width',new_value+pads);

We can also note that for :

var w1 = jqElement.css('width');
var w2 = jqelement.width();

w1 is the innerwidth, while w2 is the width (css attribute meaning) Difference which is not documented into JQ API documentation.

Best regards

Trebly


참고 : 제 생각에 이것은 버그 JQ 1.12.4로 간주 될 수 있습니다. 외출하는 방법은 'parameters'뒤에 다양한 의미가 있기 때문에 .css ( 'parameter', value)에 허용되는 매개 변수 목록을 결정하는 것입니다. 관심이 있지만 항상 명확해야합니다. 이 경우 'innerwidth'는 'width'와 동일하지 않습니다. 다음과 같은 문장으로 .width (value) 문서에서이 문제를 추적 할 수 있습니다. "현대 브라우저에서는 CSS 너비 속성에 패딩이 포함되어 있지 않습니다."

참고 URL : https://stackoverflow.com/questions/17845027/what-is-difference-between-width-innerwidth-and-outerwidth-height-innerheight

반응형