Javascript返回我的对象

Javascript返回我的对象,javascript,jquery,object,Javascript,Jquery,Object,我有以下代码使用datatables.net插件创建datatable: <!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"> <head> <meta h

我有以下代码使用datatables.net插件创建datatable:

<!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">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Untitled Document</title>
        <script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
        <script type='text/javascript' src='//netdna.bootstrapcdn.com/bootstrap/3.0.3/js/bootstrap.min.js'></script>

        <link rel="stylesheet" type="text/css" href="//netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css">
        <link type="text/css" href="https://cdn.datatables.net/1.10.3/css/jquery.dataTables.css" />
        <link type="text/css" href="https://cdn.datatables.net/plug-ins/380cb78f450/integration/bootstrap/3/dataTables.bootstrap.css" />

        <script src="https://cdn.datatables.net/1.10.3/js/jquery.dataTables.min.js"></script>
        <script src="https://cdn.datatables.net/plug-ins/380cb78f450/integration/bootstrap/3/dataTables.bootstrap.js"></script>

        <script type='text/javascript' src="https://datatables.net/release-datatables/extensions/KeyTable/js/dataTables.keyTable.js"></script>

    </head>
    <body>
        <script type="text/javascript">
            //   function day2Date( day, year ) {
            // return new Date(year,0,day);
            //}
            $(document).ready(function() {

                $('#example').dataTable({
                    "ajax": "table1.php",
                    "columns": [{
                            "data": "ID"
                        }, {
                            "data": "naziv"
                        }, {
                            "data": "vrsta"
                        },

                    ],
                    "columnDefs": [{
                        "targets": 2,
                        "data": "download_link",
                        "render": function(data, type, full, meta) {
                            // return data; 
                            return '<button class="btn btn-success">' + data + '</button>';
                        }
                    }]
                });
            });
            var table = $('#example').DataTable();
            $(document).ready(function() {
                $('#example tbody').on('click', 'td', function() {
                    console.log('Data:' + $(this).html().trim() + 'Row:' + $(this).parent().find('td').html().trim() + 'Column:' + $('#example thead tr th').eq($(this).index()).html().trim());
                    // alert('Row:'+$(this).parent().find('td').html().trim());
                    //alert('Column:'+$('#example thead tr th').eq($(this).index()).html().trim());

                });
                $('#example tbody').on('click', 'tr', function() {
                    console.log('Row index: ', table.row(this).index());
                });
            });
        </script>
        <div class="container">
            <table id="example" class="table table-striped table-bordered table-responsitive" cellspacing="0" width="100%">
                <thead>
                    <tr>
                        <th>ID</th>
                        <th>Naziv</th>
                        <th>Vrsta</th>

                    </tr>
                </thead>

                <tfoot>
                    <tr>
                        <th>ID</th>
                        <th>Naziv</th>
                        <th>Vrsta</th>
                    </tr>
                </tfoot>
            </table>
        </div>
    </body>
</html>
就像我在datatables网站上的文档中看到的一样,但此代码只返回我
[object object]

例如:

Data:12Row:2Column:Naziv 
Row index: [object Object] 

为什么??Sombody有解释吗?

当您在JavaScript对象上执行字符串连接时,它将隐式调用对象上的
toString()

默认的
Object.toString()
只返回
[Object Object]

要打印出对象的内容,请使用带有两个参数的
console.log

console.log( 'Row index:', table.row( this ).index() );
如果我在上测试,那么它看起来像预期的那样工作,结果是一个数字,所以我认为你的问题中一定缺少一些信息

var table = $('#example').DataTable()
> []
table.row(1).index()
> 1

您已经在任何DOM就绪处理程序之外包含了一行关键代码,但在它出现的元素之前。这意味着
$(“#示例”)
没有返回匹配项:

将此行放入DOM就绪处理程序:

var table = $('#example').DataTable();
e、 g


仅在对象本身上执行
控制台.log
。开发者工具随后将显示有关对象的更多信息,而不仅仅是返回
[object object]
.toString()
值。所以,做一个
console.log(table.row(this.index())
看看这个对象到底是什么。你的例子和这个例子之间唯一的区别是他们使用
alert
而不是
console.log
,你能试着把它改成
alert
看看是否会发生同样的情况吗?@Nunners:不会(或者当然不应该)有任何不同。@TrueBlueAussie我希望它不会有什么不同,所以我们至少可以把它缩小到函数调用本身@OP-你确定没有额外的代码与此相关吗?我无法在任何浏览器中重现此问题。同样,不工作,问题是其他“不工作”的东西这意味着建议的调用打印出
行索引:[object object]
?在他们的示例站点上似乎对我很好。我将更新我的答案。
行()
索引()
方法应该返回一个整数,因此这个问题似乎与
对象的字符串转换无关。
@TrueBlueAussie看起来像它。@James D:完全代码可以产生的差异令人惊讶:)
var table = $('#example').DataTable();
$(document).ready(function () {
    var table = $('#example').DataTable();
    $('#example tbody').on('click', 'td', function () {
        console.log('Data:' + $(this).html().trim() + 'Row:' + $(this).parent().find('td').html().trim() + 'Column:' + $('#example thead tr th').eq($(this).index()).html().trim());
        // alert('Row:'+$(this).parent().find('td').html().trim());
        //alert('Column:'+$('#example thead tr th').eq($(this).index()).html().trim());

    });
    $('#example tbody').on('click', 'tr', function () {
        console.log('Row index: ', table.row(this).index());
    });
});