2011年1月30日日曜日

JqueryでHTML・CSSの編集でやってみた事忘れないようにを書いてみた④

~~~~~繰り返し処理~~~~~

    <script type="text/javascript">
        $(window).load(function(){
            $("p").each(function(){
                if($(this).text() == "jQuery"){
                    $(this).text("JQUERY");
                }else{
                    $(this).text("JQUERYS");
                }
            });
        });
    </script>
①の解説
指定したセレクタのpタグの要素の要素の内が「if」もしjQueryならと処理と、ではない時の処理を
実行します。


<html>
  <head>
    <script type="text/javascript" src="jquery-1.4.3.js"charset="shift_jis"></script>
    <script type="text/javascript">
        $(window).load(function(){
            $("p").each(function(index){
                if($(this).text() == "jQuery"){
                    $(this).text("JQUERY");
                    $(this).prepend(index);
                }else{
                    $(this).text("JQUERYS");
                    $(this).prepend(index);
                }
            });
        });
    </script>
    <meta http-equiv="Content-Type" content="text/html; charset=shift_jis" />
  </head>
  <body>
  <h2></h2>
    <p>jQuery</p>
    <p>jQuery</p>
    <p>jQuery</p>
    <p>jQuerys</p>
  </body>
</html>
②の解説
指定したセレクタ要素のループ回数をfunction(index)で指定し、$(this).prepend(index);でループ回数を表示します


<html>
  <head>
    <script type="text/javascript" src="jquery-1.4.3.js"charset="shift_jis"></script>
    <script type="text/javascript">
        $(window).load(function(){
            $("p").prepend(function(index){
                if($(this).text() == "jQuery"){
                    $(this).text("JQUERY");
                    return index;
                }else{
                    $(this).text("JQUERYS");
                    return index;
                }
            });
        });
    </script>
    <meta http-equiv="Content-Type" content="text/html; charset=shift_jis" />
  </head>
  <body>
  <h2>loadが指定したセレクタの要素が読み込まれた時に実行します。</h2>
    <p>jQuery</p>
    <p>jQuery</p>
    <p>jQuery</p>
    <p>jQuerys</p>
    <script type="text/javascript">
        //document.write(top);
    </script>
  </body>
</html>
③メソッドの中にはコールバックを受け付けるメソッドもあります。prependメソッドはreturnで指定
した値を受け取ります 。







0 件のコメント:

コメントを投稿