0

私はJQueryが初めてです。私はここで JQuery の基礎を学び、配列が jQuery でどのように機能するかを真剣に知りたいと思っています。

<!DOCTYPE html>
<html>
<head>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<meta content="utf-8" http-equiv="encoding">
    <style>
  div { color:blue; }
  span { color:red; }
</style>
    <script src="jquery-latest.js"></script>
</head>
<body>

<div id="myDiv"></div>

<script>
$(document).ready(function() {

    var testArray = ["test1","test2","test3","test4"];
    var new_array=[];
    var vPool="";

    /*i tried but this pushing code dosen't work*/

   //pushing values in new_array
    $("new_array").push({"test1","test2","test3","test4"});

    //fetching the values of new_array 
    $.each(new_array, function(i, val) {
      vPool+=val + "<br />";
    });
    $('#myDiv').html(vPool);--->
    //vPool="";

    /*But i tried this code it work fine */

    //fetching the values of new_array 
    $.each(testArray, function(i, val) {
      vPool+=val + "<br />";
    });
    $('#myDiv').html(vPool); 
    //vPool="";

});
</script>

</body>
</html>

そして、PHP で印刷するように構造化配列を印刷する方法を知りたいです。

echo "<pre>";
print_r($array);
4

1 に答える 1

1

$("")周囲を取り除きnew_arrayます。また、正しく定義されていないオブジェクトをプッシュしています。また、push()関数は jQuery とは関係なく、純粋な JavaScript です。

配列全体の印刷については、console.log()Firebug 拡張機能をインストールした Chrome または Firefox で試してください。

$(document).ready(function() {

    var testArray = ["test1","test2","test3","test4"];
    var new_array=[];
    var vPool="";

    /*i tried but this pushing code dosen't work*/

    //pushing values in new_array
    new_array.push("test1","test2","test3","test4");
    console.log(new_array);

    $.each(new_array, function(i, val) {
        vPool+=val + "<br />";
        console.log(val);
    });

});

このpush()関数は、配列の新しい長さを返します。

于 2013-01-08T08:22:42.247 に答える