Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么表加载的JSON数据url在DOM中是最后一个?_Javascript_Json_Dom - Fatal编程技术网

Javascript 为什么表加载的JSON数据url在DOM中是最后一个?

Javascript 为什么表加载的JSON数据url在DOM中是最后一个?,javascript,json,dom,Javascript,Json,Dom,参考如下所示的简单HTML表: <table id="table1" data-toggle="table" data-url="data1.json" data-pagination="true" data-sort-order="desc"> data1.json: <table id="table1" data-toggle="table" data-url="data1.json"

参考如下所示的简单HTML表:

<table id="table1"
       data-toggle="table"
       data-url="data1.json"
       data-pagination="true"
       data-sort-order="desc">
data1.json:

<table id="table1"
       data-toggle="table"
       data-url="data1.json"
       data-pagination="true"
       data-sort-order="desc">
    <thead>
    <tr>
        <th data-sortable="true" data-field="name" >Name</th>
        <th data-sortable="true" data-field="W">Width</th>
        <th data-sortable="true" data-field="H">Height</th>
        <th data-sortable="true" data-field="D">Depth</th>
    </tr>
    </thead>
</table>
[{
  "name": "First Value",
  "W": 1,
  "H": 10,
  "D": 100
},{
"name": "First Value",
"W": 1,
"H": 10,
"D": 100
},{
"name": "First Value",
"W": 0,
"H": 10,
"D": 100
},...
$(document).ready(function(){
        $('td:nth-child(2)').each(function() {
            var fValue = this.text();
            if (fValue == 0) {
                var oTableRow = $(this).parent();
                oTableRow.css('background-color', 'red');
            }
        });
});
Javascript/JQuery:

<table id="table1"
       data-toggle="table"
       data-url="data1.json"
       data-pagination="true"
       data-sort-order="desc">
    <thead>
    <tr>
        <th data-sortable="true" data-field="name" >Name</th>
        <th data-sortable="true" data-field="W">Width</th>
        <th data-sortable="true" data-field="H">Height</th>
        <th data-sortable="true" data-field="D">Depth</th>
    </tr>
    </thead>
</table>
[{
  "name": "First Value",
  "W": 1,
  "H": 10,
  "D": 100
},{
"name": "First Value",
"W": 1,
"H": 10,
"D": 100
},{
"name": "First Value",
"W": 0,
"H": 10,
"D": 100
},...
$(document).ready(function(){
        $('td:nth-child(2)').each(function() {
            var fValue = this.text();
            if (fValue == 0) {
                var oTableRow = $(this).parent();
                oTableRow.css('background-color', 'red');
            }
        });
});

您遇到的问题是,您正在异步加载数据,更改脚本顺序以修复它只会进一步引入竞争条件。解决这一问题的最佳方法是在加载数据时使用钩子。您可以通过两种方式来实现这一点,一种是通过ajax手动调用json,然后加载表,以便在加载表时有一个回调。第二个选择,我认为是您最好的选择,是使用引导表发出的事件,它是onLoadSuccess

$('#table1').on('load-success.bs.table', function (e, arg1, arg2, ...) {
    // your jquery modifications go here
});

你能发布json获取部分吗。是通过ajax吗?一些JavaScript代码正在加载表内容;这不是本机浏览器功能。@hi,它只是项目根目录中的一个data1.json文件。不涉及ajax,非常标准。页面包含哪些JavaScript功能?能否向我们展示页面的整个结构?嗨,这不起作用,出于某种奇怪的原因,它错误地认为.text()不是函数。您是否尝试在jquery中包装它?i、 e$(this.text()我和我的胖手指:)