Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/476.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-DOM:html表操作问题_Javascript_Javascript Events - Fatal编程技术网

javascript-DOM:html表操作问题

javascript-DOM:html表操作问题,javascript,javascript-events,Javascript,Javascript Events,我有一张这样的桌子: <table class="inventoryItems" id="inventoryItems"> <tr> <th id="itemCost" class="noLeft">id </th> <th id="soldAmount" class="right">Item Description </th> </tr> <t

我有一张这样的桌子:

<table class="inventoryItems" id="inventoryItems">
    <tr>
        <th id="itemCost" class="noLeft">id </th>
        <th id="soldAmount" class="right">Item Description    </th>
    </tr>
    <tr id="itemRow">
        <th >
            <input onBlur="shapeMapper(this)" ></input>
        </th>
        <th >
            <input onBlur="shapeMapper(this)" ></input>
        </th>
    </tr>
</table>

身份证件
项目说明
只是一张非常简单的桌子

我试图用以下形式的脚本来操纵它:

function shapeMapper(arg){
    //arg goes unused for now.
    var tblName = "inventoryItems";
    var tbl = document.getElementById(tblName);
    var soldAmount = document.getElementById("soldAmount").cellIndex;
    var itemCost = document.getElementById("itemCost").cellIndex;
    var rowCount = tbl.rows.length;
    var itemList = "";
    var totalCost=0;
    var totalSold=0;

    for(var i = 1; i<=rowCount-1 ; i++){
        var fC = tbl.rows[i].cells[itemCost].firstChild;
        r=tbl.rows[i];

        alert(r.cells[itemCost].value);
        totalCost = totalCost + Number(r.cells[itemCost].firstChild.value) ; 
        totalSold = totalSold + Number(r.cells[soldAmount].firstChild.value) ;
    }
}
函数形状映射(arg){
//arg暂时未使用。
var tblName=“inventoryItems”;
var tbl=document.getElementById(tblName);
var soldAmount=document.getElementById(“soldAmount”).cellIndex;
var itemCost=document.getElementById(“itemCost”).cellIndex;
var rowCount=tbl.rows.length;
var itemList=“”;
var总成本=0;
var TotalSeld=0;
对于(var i=1;itry

否则它将获取一个未定义值的
textElement
,因为空格和回车符被视为DOM中的节点。
firstChild
获取第一个节点子级,而
firstElementChild
获取第一个元素子级


但是,请注意并非每个浏览器都支持
firstElementChild
(FF<3.5,即<9).

下次正确缩进。当问题本身无法读取时,没有人会解决问题。可读性是相对的…不是判断的基线
未定义的
值是未定义值时得到的值。这是最常见的期望某个成员存在,在这种情况下,
,当它oes不是。如果您的客户端代码不负责创建数据结构,请尝试
console.log(r.cells[itemCost])
(显然您需要一个实现window.console()或firefox+firebug的浏览器),以查看其中存在的数据结构。(+1)完美。非常感谢,这就是它!现在非常有意义。
r.cells[itemCost].firstElementChild.value