JS:无法读取 null 的 属性 'style'
JS: Cannot read property 'style' of null
(抱歉,我犯了一些错误,刚刚再次更新了新代码。)
我有以下代码,Chome 将 "Uncaught TypeError: Cannot read property 'style' of null" 抛出到控制台:
function gen_mainTable(){
var code = "";
code +='<table width="100%" border="1" cellspacing="0" cellpadding="4" align="center" class="FormTable_table" id="mainTable_table">';
code +='<tr><td class="CL">SELECT</td><br><td colspan="2" class="CR"><input type="radio" id="aa" name="type_group" value="0" onchange="change_mode();" checked>AA<input type="radio" id="bb" name="type_group" value="1" onchange="change_mode();" >BB</td></tr><br>';
code +='<tr id="aa_field" style="display:none"><td class="CL">TYPE_AA:
</td><td colspan="2" class="CR"><input type="text" id="type_aa" name="type_aa" maxlength="15" size="20" value=""/></td></tr><br>';
code +='<tr id="bb_field"><td class="CL">TYPE_BB:</td>
<td colspan="2" class="CR"><input type="text" id="type_bb" name="type_bb" maxlength="15" size="20" value=""/></td></tr>
$("mainTable").style.display = "";
$("mainTable").innerHTML = code;}
js 部分:
function change_mode(){
var mode = get_checked_value(get_by_name("type_group"));
if (mode == "0"){
get_by_id("aa_field").style.display = "";
get_by_id("bb_field").style.display = "none";
get_by_id("type_bb").disabled = true;
get_by_id("type_aa").disabled = false;
}else{
get_by_id("aa_field").style.display = "none";
get_by_id("bb_field").style.display = "";
get_by_id("type_aa").disabled = true;
get_by_id("type_bb").disabled = false;
}
}
更新"get_by_id"函数:
function get_by_id(id){
with(document){
return getElementById(id);
}
}
如果有人能提供帮助,我将不胜感激 :-)
好吧,我今天学到了一些东西:如果有,你不能使用 id(document.getElementById
或 document.querySelector
)从 table行数据周围没有 table
标记。
查看此演示。在第一个例子中,我选择了 bob
行,因为它在一些 table
标签中,在第二个 rose
是 null
因为它不是:
因此,要解决此问题,请将 table
元素放在行数据周围 :)
看来这是因为the HTML specification requires that a tr
must live within either a tfoot
, thead
, tbody
or table
context把它给"be acceptable"收录在了DOM.
(抱歉,我犯了一些错误,刚刚再次更新了新代码。) 我有以下代码,Chome 将 "Uncaught TypeError: Cannot read property 'style' of null" 抛出到控制台:
function gen_mainTable(){
var code = "";
code +='<table width="100%" border="1" cellspacing="0" cellpadding="4" align="center" class="FormTable_table" id="mainTable_table">';
code +='<tr><td class="CL">SELECT</td><br><td colspan="2" class="CR"><input type="radio" id="aa" name="type_group" value="0" onchange="change_mode();" checked>AA<input type="radio" id="bb" name="type_group" value="1" onchange="change_mode();" >BB</td></tr><br>';
code +='<tr id="aa_field" style="display:none"><td class="CL">TYPE_AA:
</td><td colspan="2" class="CR"><input type="text" id="type_aa" name="type_aa" maxlength="15" size="20" value=""/></td></tr><br>';
code +='<tr id="bb_field"><td class="CL">TYPE_BB:</td>
<td colspan="2" class="CR"><input type="text" id="type_bb" name="type_bb" maxlength="15" size="20" value=""/></td></tr>
$("mainTable").style.display = "";
$("mainTable").innerHTML = code;}
js 部分:
function change_mode(){
var mode = get_checked_value(get_by_name("type_group"));
if (mode == "0"){
get_by_id("aa_field").style.display = "";
get_by_id("bb_field").style.display = "none";
get_by_id("type_bb").disabled = true;
get_by_id("type_aa").disabled = false;
}else{
get_by_id("aa_field").style.display = "none";
get_by_id("bb_field").style.display = "";
get_by_id("type_aa").disabled = true;
get_by_id("type_bb").disabled = false;
}
}
更新"get_by_id"函数:
function get_by_id(id){
with(document){
return getElementById(id);
}
}
如果有人能提供帮助,我将不胜感激 :-)
好吧,我今天学到了一些东西:如果有,你不能使用 id(document.getElementById
或 document.querySelector
)从 table行数据周围没有 table
标记。
查看此演示。在第一个例子中,我选择了 bob
行,因为它在一些 table
标签中,在第二个 rose
是 null
因为它不是:
因此,要解决此问题,请将 table
元素放在行数据周围 :)
看来这是因为the HTML specification requires that a tr
must live within either a tfoot
, thead
, tbody
or table
context把它给"be acceptable"收录在了DOM.