Méthode jQuery ...SE CONNECTER

Méthode jQuery css()

La méthode

css() définit ou renvoie un ou plusieurs attributs de style de l'élément sélectionné.

Renvoyer les propriétés CSS

Pour renvoyer la valeur d'une propriété CSS spécifiée, veuillez utiliser la syntaxe suivante :

css("propertyname");

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
  $("button").click(function(){
    alert("背景颜色 = " + $("p").css("background-color"));
  });
});
</script>
</head>
<body>
<h2>这是一个标题</h2>
<p style="background-color:#ff0000">这是一个段落。</p>
<p style="background-color:#00ff00">这是一个段落。</p>
<p style="background-color:#0000ff">这是一个段落。</p>
<button>返回 p 元素的 background-color </button>
</body>
</html>

Définir les propriétés CSS

Pour définir des propriétés CSS spécifiques, veuillez utiliser la syntaxe suivante :

css("propertyname","value");

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
  $("button").click(function(){
    $("p").css("background-color","yellow");
  });
});
</script>
</head>
<body>
<h2>标题</h2>
<p style="background-color:#ff0000; width:200px;">段落1</p>
<p style="background-color:#00ff00; width:200px;">段落2</p>
<p style="background-color:#0000ff; width:200px;">段落3</p>
<p style="width:300px;">段落4</p>
<button>设置 p 元素的背景</button>
</body>
</html>

Définir plusieurs propriétés CSS

Pour définir plusieurs propriétés CSS, veuillez utiliser la syntaxe suivante :

css({"propertyname":"value ", "propertyname": "value",...});

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
  $("button").click(function(){
    $("p").css({"background-color":"yellow","font-size":"200%"});
  });
});
</script>
</head>
<body>
<h2>标题</h2>
<p style="background-color:#ff0000; width:200px;">段落1</p>
<p style="background-color:#00ff00; width:200px;">段落2</p>
<p style="background-color:#0000ff; width:200px;">段落3</p>
<p style="width:300px;">段落4</p>
<button>设置 p 元素的背景</button>
</body>
</html>



section suivante
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> <script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("button").click(function(){ $("div").css({color:"red",fontSize:20,backgroundColor:"yellow"}); }) }); </script> <style type="text/css"> div{ color:blue; background-color:green; width:100px; height:100px; margin-top:5px; } </style> </head> <body> <div>仔细查看变化</div> <div>仔细查看变化</div> <br> <button>点击查看效果</button> </body> </html>
soumettreRéinitialiser le code
chapitredidacticiel