2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

jQueryでCSSを使う方法

Posted at
こちらは初心者向けです。

jQueryでCSSを使う

CSSファイルやスタイルシートを定義しなくてもjQueryでデザインを定義、変更することができます。
こちらをできるようにする関数は「.css()」です。

css.html
$(".css-div").css("width", "100px").css("border", "#000 2px solid");

.css(///).css(///)
こんな感じでcssをずっとつないで定義することもできまして

css.html
$(".css-div").css({
  width: "100px",
  border: "#000 2px solid"
});

のように1つの「.css()」の中に複数のスタイルを定義することもできます。

ボタンを押したら線の色が変わることを作ってみましょう。
コードはこちらです。

css.html
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>

<p><button>変更</button></p>

<div class="css-div">こんばんは</div>

<script type="text/javascript">
$(document).ready(function() {
  $(".css-div").css({
    width: "100px",
    border: "#000 2px solid",
    padding: "5px"
  });
  $(document).on("click", "button", function() {
    $(".css-div").css("borderColor", "#f00");
  });
});
</script>

Cap 2016-06-06 18-47-21-344.png

ボタンを押すと線の色が赤に変更します。

Cap 2016-06-06 18-47-26-373.png

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?