PHP & JavaScript Room :: 設置サンプル

実行結果

DOM選択処理:調査:children()の使用例 | jQuery

▼下記の要素をクリックすると、クリックした要素の直下の子要素を赤枠で囲います。

This is the way we write the demo,

write the demo, demo,
This the way we write the demo so in
クリックしたTAG要素の中に0つの子要素が見つかりました。

設置サンプルのソース

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja" lang="ja">
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
      <meta http-equiv="Content-Language" content="ja" />
      <meta http-equiv="Content-Script-Type" content="text/javascript" />
      <meta http-equiv="Content-Style-Type" content="text/css" />
      <meta http-equiv="imagetoolbar" content="no" />
      <title>DOM選択処理:調査:children()の使用例 | jQuery</title>
      <link rel="stylesheet" type="text/css" href="/content/lib/global.css" />
      <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
      <script type="text/javascript">
         $(function(){
            /* クリックした要素の子要素をすべて探す */
            $("#sample").click(function(e){
               $("*").removeClass("hilite");
               var $kids = $(e.target).children();
               var len = $kids.addClass("hilite").length;
               $("#res strong:first").text(e.target.tagName);
               $("#res strong:last").text(len);
               e.preventDefault();
               return false;
            });
         });
      </script>
      <style type="text/css">
         #sample div { margin:0 10px 1em 0; padding:10px; background-color:#fff; border:3px solid #ccc; width:150px; float:left; height:8em; }
         #sample .hilite { border:1px solid red; }
         #sample span { color:blue; border:1px solid transparent; }
         #sample em { border:1px solid transparent; }
         #sample a { border:1px solid transparent; }
         #sample b { border:1px solid transparent; }
         #res { color:red; }
      </style>
   <link rel="stylesheet" type="text/css" href="/common/css/example.css"></head>
   <body id='example3' class='example'><div class="ads" style="margin:32px auto;text-align:center;"></div><h1 class='h'><a href='/'>PHP &amp; JavaScript Room</a> :: 設置サンプル</h1>
<h3 class='h'>実行結果</h3>
      <div id="wrap">
         <h1>DOM選択処理:調査:children()の使用例 | jQuery</h1>
         <p>▼下記の要素をクリックすると、クリックした要素の直下の子要素を赤枠で囲います。</p>
<!-- CODE -->
         <div id="sample" class="cf">
            <div>
               <p>This <span>is the <em>way</em> we</span> write <em>the</em> demo,</p>
            </div>
            <div>
               <a href="#"><b>w</b>rit<b>e</b></a> the <span>demo,</span> <button>write the</button> demo,
            </div>
            <div>
               This <span>the way we <em>write</em> the <em>demo</em> so</span><input type="text" value="early" /> in
            </div>
         </div>
         <span id="res">クリックした<strong>TAG</strong>要素の中に<strong>0</strong>つの子要素が見つかりました。</span>
<!-- / CODE -->
      </div>
   </body>
</html>