0

dataTable関数の初期化をオブジェクトの内部に配置しましたが、オブジェクトの外部で初期化した場合と同じ結果が得られません

オブジェクトの外部での初期化

var dataTable = $('datatable').dataTable();

オブジェクト内の初期化

var aObject = {
    dataTable : null,
    initFunction : function() {
        // this.dataTable contents is not the same when I initialize dataTable outside the object
        this.dataTable = $('datatable').dataTable();
    }
}

どうしてこれなの?

編集:また、オブジェクト内で実行すると、テーブルをdataTablesに正常に初期化できないようです。

4

1 に答える 1

0

次のコードを使用してテストしましたが、両方のconsole.logの結果は同じです。両方の場所(オブジェクトの内側と外側)で正しいセレクターを使用していることを確認しますか?

var aObject = {
    dataTable : null,
    initFunction : function() {
        this.dataTable = $('#data').dataTable();
        }
    }

    $(function() {
        var x = $("#data").dataTable();
        aObject.initFunction();

        console.log(x);
        console.log(aObject.dataTable);
    })

および次のhtmlテーブル:

<table id="data">
    <thead>
        <tr>
            <th>Name</th>
            <th>Age</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>
                one
            </td>
            <td>
                22
            </td>
        </tr>
    </tbody>
</table>
于 2011-09-05T08:59:08.540 に答える