Home > Web Front-end > JS Tutorial > body text

How to set css width using jquery

醉折花枝作酒筹
Release: 2023-01-05 16:08:39
Original
2634 people have browsed it

In jquery, you can use the "css()" method to set the width, the syntax is "$(object).css("width","size value")"; the "css()" method sets or returns One or more style attributes of the selected element. This is a best way to set a large number of style attributes on all matching elements.

The operating environment of this tutorial: Windows 7 system, jquery version 1.11.1, Dell G3 computer.

Set the width of the

element:

<!DOCTYPE html>
<html>
<head>
<script src="/jquery/jquery-1.11.1.min.js"></script>
<script>
$(document).ready(function(){
  $("button").click(function(){
    $("p").css("width","200px");
  });
});
</script>
</head>

<body>
<h2>这是标题</h2>
<p style="background-color:#ff0000">这是一个段落。</p>
<p style="background-color:#00ff00">这是一个段落。</p>
<p style="background-color:#0000ff">这是一个段落。</p>
<p>这是一个段落。</p>
<button>设置 p 元素的宽度</button>
</body>
</html>
Copy after login

Run result:

How to set css width using jquery

##[Recommended learning:

javascript video tutorial

The above is the detailed content of How to set css width using jquery. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template