HOME > > > > >

jQuery()

jQuery( selector [, context] )

引数

(string)expression
検索式。構文はselectorを参照
(string|jQuery)context
検索対象。省略時は現在のHTMLDOM。

戻り値

jQuery
jQueryオブジェクト

超重要。jQueryの基本となる関数。これを実行するとjQueryオブジェクトが返ってくる。Core以外の関数はjQueryオブジェクトのメソッド。

サンプル

実行結果

jQueryテスト

Javascript

  1. // .jqTestにボーダー
  2. $('.jqTest1 .execute').click(function(){
  3.     $('.jqTest1 p').css('border', '1px solid #000000');
  4. });

HTML

  1.  
  2. <div class="jqTest1">
  3.     <input type="button" value="ボーダー設定" class="execute" />
  4.     <p>jQueryテスト</p>
  5. </div>

contextを指定することで検索範囲を絞ることができる。contextにはjQueryオブジェクト、selectorを渡せる。

サンプル

実行結果

jQueryテスト outer

jQueryテスト inner

Javascript

  1. // .jqTest2内からp要素を取得
  2. $('.execute1', '.jqTest2').click(function(){
  3.     $('p', $('.jqTest2')).css('backgroundColor', '#CCCCCC');
  4. });
  5.  
  6. $('.execute2', '.jqTest2').click(function(){
  7.     $('p', '.jqTest2 .inner').css('backgroundColor', '#990000');
  8. });

HTML

  1.  
  2.  
  3. <div class="jqTest2">
  4.     <input type="button" value="jqTest2内のpの背景色変更" class="execute1" />
  5.     <input type="button" value="jqTest2 .inner 内のpの背景色変更" class="execute2" />
  6.     <p>jQueryテスト outer</p>
  7.     <div class="inner">
  8.         <p>jQueryテスト inner</p>
  9.     </div>
  10. </div>

jQuery( html [, ownerDocument] )

引数

html
html
ownerDocument
生成する要素のドキュメント。いまいち不明。

戻り値

jQuery
jQueryオブジェクト

htmlからjQueryオブジェクトを作成する

サンプル

実行結果

  • test3

Javascript

  1. $('.jqTest3 .execute').click(function(){
  2.     var lis = $('<li>test</li><li>test2</li>');
  3.     $('.jqTest3 ul').append(lis);
  4. });

HTML

  1. <div class="jqTest3">
  2.     <input type="button" value="LI追加" class="execute" />
  3.     <ul>
  4.     <li>test3</li>
  5.     </ul>
  6. </div>

jQuery( html, props )

引数

html
html
(object)props
作成される要素の属性、イベントハンドラが設定できる。

戻り値

jQuery
jQueryオブジェクト

くっそ便利。

サンプル

実行結果

Javascript

  1. $('.jqTest4 .execute').click(function(){
  2.     $('.jqTest4').append($('<button>', {
  3.         html:  '実行',
  4.         class: 'execute2',
  5.         click: function(){
  6.                    alert($(this).text());
  7.                }
  8.     })).append($('<p>', {
  9.         html: '上のボタンを押すと何かおこる',
  10.     }).css({
  11.         fontWeight: 'bold',
  12.         color: 'red'
  13.     }));
  14. });

HTML

  1. <div class="jqTest4">
  2.     <input type="button" value="ボタン追加" class="execute" />
  3. </div>

IEの場合、input要素の属性変更はできないらしい。

Note: Internet Explorer will not allow you to create an input or button element and change its type; you must specify the type using '<input type="checkbox" />' for example. A demonstration of this can be seen below:

jQuery( html [, ownerDocument] )

jQuery( callback )

引数

(function)callback
DOMの構築が完了したときに実行される関数

詳しくはjQuery( callback )

jQueryオブジェクト

jQuery関数を実行して返ってくるjQueryオブジェクトの中身は下記。

サンプル

実行結果

jQuery オブジェクト

Javascript

  1. var a = $('p', $('.jqTest5'));
  2. var t = '';
  3.  
  4. for (var b in a) {
  5.     t += '<span class="bold">' + b + "</span> =" + a[b] + "<br /><br />";
  6. }
  7.  
  8. $('.jqTest5').html(t);

HTML

  1. <div class="jqTest5">
  2.     <p>jQuery オブジェクト</p>
  3. </div>