jQuery HTML

4769 ワード

Get Content and Attributes

  • text():テキストの内容だけで
  • html():渡された値はhtml自体のタグ
  • を含む.
  • val()
  • attr():選択した要素属性の値
  • を取得します.

    Set Content and Attributes

  • text()
  • html()
  • val()
  • attr()
  • $("#btn1").click(function(){
      $("#test1").text("Hello world!");
    });
    $("#btn2").click(function(){
      $("#test2").html("<b>Hello world!</b>");
    });
    $("#btn3").click(function(){
      $("#test3").val("Dolly Duck");
    });

    Add Elements

  • append()
  • prepend()
  • after()
  • before()
  • <script>
    $(document).ready(function(){
      $("#btn1").click(function(){
        $("p").append(" <b>Appended text</b>.");
      });
    
      $("#btn2").click(function(){
        $("ol").prepend("<li>Appended item</li>");
      });
    });
    </script>
    差異
  • 以降、beforeはタグの外部に追加され、
  • .
  • append,prependは対応するタグに
  • 追加される.

    Remove Elements

  • remove():選択した要素+childを削除(
  • )
  • empty():child要素
  • のみ削除

    Get and Set CSS Classes

  • addClass() - Adds one or more classes to the selected elements
  • removeClass() - Removes one or more classes from the selected elements
  • toggleClass() - Toggles between adding/removing classes from the selected elements
  • css() - Sets or returns the style attribute
    * css("propertyname","value");
  • css({"propertyname":"value","propertyname":"value",...});
  • jQuery Dimensions