【问题标题】:textContent results in invalid string in Javascript arraytextContent 导致 Javascript 数组中的字符串无效
【发布时间】:2017-07-10 17:25:38
【问题描述】:

如果html表中一行的第3个字段是

所有“狗”都去天堂。

浏览器控制台将显示以下内部列表(在调用函数 filldb() 之后)。请注意表的该行/字段和结果列表元素中的 textContent 中的不匹配的内部引号

["1", "101", "3/S4.1", "All "dogs' go to heaven.", "4*3*2", "5", "60", "1", "30-00.0", "750.96"]

 function filldb(){
    estRows=[];
    rows=e_tbody.rows;
    // 'rows' is now the collection of rows in your dynamically output table.
    var len=rows.length;
    var lnItm=[];
    for(var i=0; i<len; i++){
        lnItm=[];
        lnItm.push(rows[i].cells[0].textcontent);
        lnItm.push(rows[i].cells[1].textcontent);
        lnItm.push(rows[i].cells[2].textcontent);
        lnItm.push(rows[i].cells[3].textcontent);
        --- <snip> ---
        lnItm.push(rows[i].cells[9].textcontent);
        estRows.push(lnItm);
    }
    for(var i=0; i<len; i++){
        console.log(estRows[i]);
    }
}

我很困惑 JavaScript 数组的元素如何成为无效字符串。解释?关于 textContent 没有被解析的问题?

【问题讨论】:

  • JavaScript 区分大小写 -> textcontent !== textContent
  • "All "Dogs' go to heaven." 该报价预计将在天堂之后关闭。但它在 Dogs 之前就关闭了。
  • "All "Dogs' go to heaven." 应该类似于 "All \"Dogs' go to heaven."

标签: javascript html arrays html-table


【解决方案1】:

当然解析了,看控制台 ["a", "All "Dogs' go to Heaven."] 里面的字符串"是红色的,其他的都是黑色的,这是日志数组的规则,你可以使用JSON.stringify 将所有内容记录为字符串

var arr = ['a']
arr.push(`All "Dogs' go to heaven.`)
console.log(arr)
console.log(JSON.stringify(arr))

【讨论】:

    猜你喜欢
    • 2019-05-13
    • 2019-02-28
    • 2012-01-01
    • 2020-03-14
    • 1970-01-01
    • 1970-01-01
    • 2020-02-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多