javascript 'style' 为空或不是对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6857257/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 22:07:44  来源:igfitidea点击:

'style' is null or not an object

javascriptjquery-datatables

提问by EKS

Im having some issues with datatables.net and internet explorer 8 ( Could also be other browsers, but works in IE9). I have spent some time trying to find out what the issue is and i have been unable, but i have figured out what javascript that seems to trigger it:

我在使用 datatables.net 和 Internet Explorer 8 时遇到了一些问题(也可能是其他浏览器,但适用于 IE9)。我花了一些时间试图找出问题所在,但我一直无法解决,但我已经弄清楚似乎是什么 javascript 触发了它:

If i remove this code then it works in IE 8, can someone point out the error in my ways ?

如果我删除此代码,那么它可以在 IE 8 中使用,有人可以指出我的错误吗?

"aoColumns": [
    { "sType": "string" },                       // Player name
    { "sType": "numeric-minus" },                       // Damage done
    { "sType": "numeric-comma", "bVisible": false },    // DPS real
    { "sType": "numeric-comma" },                       // DPS Avg
    {"sType": "numeric-minus" },                        // Damage taken
    {"sType": "numeric-minus" },                        // Healing done
    {"sType": "numeric-comma", "bVisible": false },    // healing done HPS
    {"sType": "numeric-comma" },    // healing done HPS Avg
    { "sType": "numeric-comma" },                       // Overhealing
    { "sType": "numeric-comma" },                       // Healing taken
    { "sType": "numeric-comma", "bVisible": false },    // Mana done
    { "sType": "numeric-comma", "bVisible": false },    // Stamina done
    {"sType": "string", "bVisible": false },            // Class
    {"sType": "percent" },                              // Activity
],

Error details from IE 8
Webpage error details

来自 IE 8
网页错误详情的错误详情

User Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET4.0C; .NET4.0E)
Timestamp: Thu, 28 Jul 2011 09:59:45 UTC    

Message: 'style' is null or not an object
Line: 5585
Char: 7
Code: 0
media/js/jquery.dataTables.js

Lines from datatable around error ( Error line has comment behind it ).

数据表中围绕错误的行(错误行后面有注释)。

Function: _fnGetUniqueThs Purpose: Get an array of unique th elements, one for each column
Returns: array node:aReturn - list of unique ths
Inputs: object:oSettings - dataTables settings object
node:nHeader - automatically detect the layout from this node - optional
array object:aLayout - thead/tfoot layout from _fnDetectHeader - optional

函数:_fnGetUniqueThs 目的:获取唯一的第 th 个元素的数组,每列一个
返回:array node:aReturn - 唯一的 ths 列表
输入:object:oSettings - dataTables 设置 object
node:nHeader - 自动检测来自该节点的布局 - 可选
数组对象:aLayout - 来自 _fnDetectHeader 的 thead/tfoot 布局 - 可选

var nThs = _fnGetUniqueThs( oSettings, nTheadClone );
iCorrector = 0;
for ( i=0 ; i<iColums ; i++ )
{
    var oColumn = oSettings.aoColumns[i];
    if ( oColumn.bVisible && oColumn.sWidthOrig !== null && oColumn.sWidthOrig !== "" )
    {
        nThs[i-iCorrector].style.width = _fnStringToCss( oColumn.sWidthOrig );
    }
    else if ( oColumn.bVisible )
    {
        nThs[i-iCorrector].style.width = ""; // This is the error line
    }
    else
    {
        iCorrector++;
    }
}

采纳答案by EKS

Using this code fixed the problem for me, il leave the question open perhaps somone knows why it works with this change.

使用此代码为我解决了问题,我将问题悬而未决,也许有人知道为什么它适用于此更改。

"aoColumns": [
    { "sType": "string", "sWidth": "auto" },                       // Player name
    {"sType": "numeric-minus", "sWidth": "auto" },                       // Damage done
    {"sType": "numeric-comma", "bVisible": false, "sWidth": "auto" },    // DPS real
    {"sType": "numeric-comma", "sWidth": "auto" },                       // DPS Avg
    {"sType": "numeric-minus", "sWidth": "auto" },                        // Damage taken
    {"sType": "numeric-minus", "sWidth": "auto" },                        // Healing done
    {"sType": "numeric-comma", "bVisible": false, "sWidth": "auto" },    // healing done HPS
    {"sType": "numeric-comma", "sWidth": "auto" },    // healing done HPS Avg
    {"sType": "numeric-comma", "sWidth": "auto" },                       // Overhealing
    {"sType": "numeric-comma", "sWidth": "auto" },                       // Healing taken
    {"sType": "numeric-comma", "bVisible": false, "sWidth": "auto" },    // Mana done
    {"sType": "numeric-comma", "bVisible": false, "sWidth": "auto" },    // Stamina done
    {"sType": "string", "bVisible": false, "sWidth": "auto" },            // Class
    {"sType": "percent", "sWidth": "auto" }                              // Activity
],

回答by Adam Field

The problem is most likely your last object in the aoColumns array:

问题很可能是您在 aoColumns 数组中的最后一个对象:

    {"sType": "percent" },   
],

You've left the comma on the last entry. I made the same error and it worked happily with Firefox at least, but not IE 8.

您在最后一个条目上留下了逗号。我犯了同样的错误,它至少在 Firefox 上运行得很愉快,但在 IE 8 上却没有。

回答by Hernan Marcelo

in my case using width="100%"in table caused the problem, removing width solved it.

在我的情况下,使用width="100%"in table 导致了问题,删除宽度解决了它。

working code

工作代码

<table id="dt_table">
<thead>
<tr>
    <th>column1</th>
    <th>column2</th>
    <th>column3</th>
    <th>column4</th>
    <th>column5</th>
    <th>column6</th>
</tr>
</thead>
</table>