LoginSignup
1
2

More than 5 years have passed since last update.

JQuery【メモ】

Last updated at Posted at 2016-07-26

JQueryのメモ

値をセットする

<html lang="ja">
    <head>
        <meta charset="uTF-8">
        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js""></script>
        <title>さんぷる</title>
    </head>
    <body>
        <script> 
        $(function() {
            $('#text_update').text("ああああ");
            $('#update_input_text').val("123456");
            $('#update_html').html('<b>aaa</b>');
        });
        </script>
        <span id="text_update"></span>
        <form method="post">
            入力:<input type="text" name="namae" size="40" id="update_input_text">
        </form>
        <p id="update_html"></p>
    </body>
</html>

valとtextで間違えやすいので注意。
テキストをそのまま置く場合にはtextを使う。

追記
タグがあるときはシングルコーテーションで囲った方がいいかも。(上記ソース修正済)
このままgithubにあげたら警告?(コードが赤くなる)が出たので。

~.clickと$(document).onの違い

<html lang="ja">
    <head>
        <meta charset="uTF-8">
        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js""></script>
        <title>さんぷる</title>
    </head>
    <body>
        <script> 
        $(function() {
            $('.button').click(function(){
                 alert("clickイベント");
            });

            $(document).on('click', '.button2', function(){alert("onイベント");});

        });
        </script>
        <a href="" class="button">click</a><br>
        <a href="" class="button2">on</a>
    </body>
</html>

~.click:その要素に対してイベント発生。FW等でまだ描画してない場合、完了しない。
$(document).on:ページ全体を描画してからイベント発生。
※ソースだと違いはわかりません。(書き方忘れそうだったから追加)

1
2
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
1
2