Javascript 浏览器有时会添加<;t车身>;
我看到了一个类似的问题,但不确定是否有解决办法,而且情况略有不同,所以我想我会再问一次 我有一个包含PHP和JavaScript的页面。PHP从数据库中检索信息,并使用它创建一个复选框。此复选框包装在表格中,以确保格式正确。页面上有一个按钮,按下时,JavaScript会创建一个新的复选框,其格式与PHP中创建的格式相同。我有一些代码,可以查看是否按下了任何复选框:Javascript 浏览器有时会添加<;t车身>;,javascript,php,html,checkbox,Javascript,Php,Html,Checkbox,我看到了一个类似的问题,但不确定是否有解决办法,而且情况略有不同,所以我想我会再问一次 我有一个包含PHP和JavaScript的页面。PHP从数据库中检索信息,并使用它创建一个复选框。此复选框包装在表格中,以确保格式正确。页面上有一个按钮,按下时,JavaScript会创建一个新的复选框,其格式与PHP中创建的格式相同。我有一些代码,可以查看是否按下了任何复选框: alert(selectedCheckBoxWrapper.firstChild.firstChild.firstChild.ch
alert(selectedCheckBoxWrapper.firstChild.firstChild.firstChild.checked);
正如您所看到的,代码试图查看
包装器内部,并确定复选框是否选中。我遇到的问题是,在PHP中创建的复选框与在JavaScript中创建的圆锥体不同
PHP复选框:
<table style="visibility: visible; position: absolute; top: 104px; left: 177px; width: 150px; height: 25px;" id="obj1" border="0" name="btn_177_1_test1">
<tbody>
<tr>
<td valign="middle" width="1">
<input type="checkbox" id="obj1_child" disabled="">
</td>
<td valign="middle" style="font-size: 13px;">
<label for="obj1_child">test1</label>
</td>
</tr>
</tbody>
</table>
<table style="visibility: visible; position: absolute; top: 216px; left: 166px; width: 150px; height: 25px;" id="obj2" border="0" name="btn_177_1_test2" >
<tr>
<td valign="middle" width="1">
<input type="checkbox" id="obj2_child" disabled="">
</td>
<td valign="middle" style="font-size: 13px;">
<label for="obj2_child">test2</label>
</td>
</tr>
</table>
echo "<table style=\"visibility: " . $part['visible'] . "; position: absolute; top: " . $part['top'] . "px; left: " . $part['left'] . "px; width: " . $part['width'] . "px; height: " . $part['height'] . "px;\" id='obj" . $part['part_id'] . "' border=0 name='btn_" . $part['stacks_id'] . "_" . $part['cards_id'] . "_" . $part['name'] . "'>
<tr>
<td valign='middle' width=1>
<input type='checkbox' id=\"obj" . $part['part_id'] . "_child\" " . $part['disabled'] . ">
</td>
<td valign=\"middle\" style=\"font-size: 13px;\">
<label for=\"obj" . $part['part_id'] . "_child\">" . $part['name'] . "</label>
</td>
</tr>
</table>";
var newTableElement = document.createElement( "table" );
newTableElement.setAttribute('style', newElementStyle );
newTableElement.setAttribute('id', newElementID );
newTableElement.border = 0;
newTableElement.setAttribute('name', newElementName );
var newTableElementRow = document.createElement( "tr" );
newTableElement.appendChild( newTableElementRow );
var newTableElementCell1 = document.createElement( "td" );
newTableElementCell1.setAttribute("valign", "middle" );
newTableElementCell1.width = 1;
newTableElementRow.appendChild( newTableElementCell1 );
var newTableElementCell2 = document.createElement( "td" );
newTableElementCell2.setAttribute("valign", "middle" );
newTableElementCell2.style.fontFamily = "'Lucida Grande', Verdana, Arial, sans-serif;";
newTableElementCell2.style.fontSize = "13px";
newTableElementCell2Label = document.createElement( "label" );
newTableElementCell2Label.setAttribute('for', whatElement.getAttribute('id') + "_child" );
newTableElementCell2Label.innerHTML = innerContents;
newTableElementCell2.appendChild( newTableElementCell2Label );
newTableElementRow.appendChild( newTableElementCell2 );
var newElement = document.createElement( "input" );
newElement.setAttribute('type', 'checkbox' );
newElement.setAttribute('id', whatElement.getAttribute('id') + "_child" );
newTableElementCell1.appendChild( newElement );
newelementStyle和innerContents等变量只包含预设值
我的代码有问题吗?或者有没有办法告诉浏览器不要添加
标记?当使用JavaScript创建表
元素时,您需要插入tbody
元素,以匹配HTML解析器创建的表
元素的结构。HTML标记不需要有tbody
标记,但是tbody
元素就在那里
因此,您只需修改JavaScript代码,以便在创建
表
元素后,它创建一个tbody
元素,使其成为表
的子元素,然后使所有tr
元素成为tbody
的子元素,不是表
使用JavaScript创建表
元素时,需要插入tbody
元素,以便与HTML解析器创建的表
元素的结构相匹配。HTML标记不需要有tbody
标记,但是tbody
元素就在那里
因此,您只需修改JavaScript代码,以便在创建
表
元素后,它创建一个tbody
元素,使其成为表
的子元素,然后使所有tr
元素成为tbody
的子元素,不是表
您可以在stackoverflow post中看到为什么需要tbody的答案:您可以在stackoverflow post中看到为什么需要tbody的答案: