width

« Обратно к таблицам стилей CSS

width( )

Получает текущее значение ширины в пикселях для первого совпавшего элемента в наборе.
Начиная с jQuery версии 1.2 этот метод также позволяет находить ширину для объектов window и document.
Примеры:
Показывает несколько значений ширины. Обратите внимание, что значения могут быть немного меньшими, чем Вы ожидали, поскольку значения берутся из фреймов. Желтый цвет показывает тело фрейма.
    function showWidth(ele, w) {
      $("div").text("The width for the " + ele +
                    " is " + w + "px.");
    }
    $("#getp").click(function () {
      showWidth("paragraph", $("p").width());
    });
    $("#getd").click(function () {
      showWidth("document", $(document).width());
    });
    $("#getw").click(function () {
      showWidth("window", $(window).width());
    });
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
                    "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
  <script src="http://code.jquery.com/jquery-latest.js"></script>

  <script>
  $(document).ready(function(){

    function showWidth(ele, w) {
      $("div").text("The width for the " + ele +
                    " is " + w + "px.");
    }
    $("#getp").click(function () {
      showWidth("paragraph", $("p").width());
    });
    $("#getd").click(function () {
      showWidth("document", $(document).width());
    });
    $("#getw").click(function () {
      showWidth("window", $(window).width());
    });

  });
  </script>

  <style>
  body { background:yellow; }
  button { font-size:12px; margin:2px; }
  p { width:150px; border:1px red solid; }
  div { color:red; font-weight:bold;  }
  </style>
</head>
<body>
  <button id="getp">Get Paragraph Width</button>

  <button id="getd">Get Document Width</button>
  <button id="getw">Get Window Width</button>
  <div> </div>
  <p>

    Sample paragraph to test width
  </p>
</body>
</html>
Name Type

width( значение )

Устанавливает свойство ширины стиля CSS для каждого элемента в наборе.
Если не было указано явного объекта (например, ‘em’ или ‘%’), тогда к указанному значению добавляется приставка “px”.
Аргументы:
значение Строка, Число
Значение свойства стиля CSS ‘width’.
Примеры:
При нажатии ЛКМ, изменяет ширину элемента div до 30px, а также изменяется цвет фона.

    $("div").one('click', function () {
      $(this).width(30)
             .css({cursor:"auto", "background-color":"blue"});
    });
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
                    "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
  <script src="http://code.jquery.com/jquery-latest.js"></script>

  <script>
  $(document).ready(function(){

    $("div").one('click', function () {
      $(this).width(30)
             .css({cursor:"auto", "background-color":"blue"});
    });

  });
  </script>

  <style>
  div { width:70px; height:50px; float:left; margin:5px;
        background:red; cursor:pointer; }
  </style>
</head>
<body>
  <div></div>
  <div>d</div>

  <div>d</div>
  <div>d</div>
  <div>d</div>
</body>

</html>
Name Type