无法检索 HTML 集合长度

洛萨

// this sets the inventory figures

function ricercaInCol(tableId,colonna,value){

    const table = document.getElementById('table'); 
    const n_colonne = table.getElementsByTagName('th').length;
    document.write(n_colonne);
    const celle = table.getElementsByTagName('td');
    let contatore = 0;
    let occurrMatch = 0;
    for(let j=colonna; j<celle.length; j=(j+n_colonne)){
         if(celle[j].innerText.match(value)){
           occurrMatch++;
           contatore++;
         }
    }
    return occurrMatch;
  };
  ricercaInCol(table,3,'G7');
body {
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    background-color: beige;
}
button {
    margin-left: 10px;
}
h1 {
    text-align: center;
}
h2 {
    text-align: center;
}
a {
    text-decoration: none;
}
a:hover {
    color: brown;
}
table {
    border-collapse: collapse;
}  
td {
    border: 1px solid black;
    text-align: center;
}
th {
    background-color: #4CAF50;
    border: 1px solid white;
    text-align: center;
    color: white;
    padding: 10px;
}
tr:nth-child(odd) {
		background-color: rgb(198, 244, 209);
        color:black;
}
.trAlternate {
    background-color: rgb(108, 117, 202);
        color:black;
}
tr:hover {background-color: #f5f5f5;}
<!DOCTYPE html>
<html lang="en">
<head>
    <title>Hardware Inventory</title>
    <meta charset="utf-8">
    <link rel="stylesheet" href="css/style.css">

</head>
<body>
    <h1>INVENTORY TEST</h1>
    <h2></h2>
    <table id="table" class="sortable"></table>

<script src="js/d3.min.js"></script>
<script src="js/d3run.js" charset="utf-8"></script>
<script src="js/sorttable.js" charset="utf-8"></script>
<script src="js/inventory.js" charset="utf-8"></script>
</body>
</html>

我有一个需要检索 HTML 集合长度的函数。但是,当我运行时:

const n_colonne = document.getElementsByTagName('th').length; 

返回的值为 0。而当我去的时候:

const n_colonne = document.getElementsByTagName('th'); 

我得到了完整的集合。脚本被放置在 body 结束标记之前的 body 末尾。该表(由 D3.js 从 csv 文件动态生成)放置在脚本之前。我知道这些症状是在 DOM 完成元素渲染之前加载的脚本的典型特征。但这种情况并非如此。我不明白这怎么会发生。

function ricercaInCol(tableId,colonna,value){
    const table = document.getElementById('tableId'); 
    const n_colonne = table.getElementsByTagName('th').length;
    const celle = table.getElementsByTagName('td');
    let contatore = 0;
    let occurrMatch = 0;
    for(let j=colonna; j<celle.length; j=(j+n_colonne)){
         if(celle[j].innerText.match(value)){
           occurrMatch++;
           contatore++;
         }
    }
    return occurrMatch;
  };
洛萨

对于那些像我一样从文件(在这种情况下为 csv)动态生成 html 表并且不知道如何处理(尚)动态生成的元素不可选择的情况的人,因为在脚本运行时尚不可用:

  • 确保在 html 中说明这些元素。解析完成后,不要使用 APPEND 到 DOM,而是使用 SELECT(现有元素)并将数据传递给它们。在我的情况下,我无法选择第 th 个标签并将它们插入到 html 中。所以生成表的函数可以是这样的:

     var tabulate = function (data,columns) {
    var table = d3.select('#table'); // here select rather than append
      var thead = table.select('thead') // here select rather than append
      var tbody = table.select('tbody') // here select rather than append
    
      thead.select('tr') // here select rather than append
        .selectAll('th')
          .data(columns)
          .text(function (d) { return d })
    
      var rows = tbody.selectAll('tr')
          .data(data)
          .enter()
        .append('tr')
    
      var cells = rows.selectAll('td')
          .data(function(row) {
              return columns.map(function (column) {
                  return { column: column, value: row[column] }
            })
        })
        .enter()
      .append('td')
        .text(function (d) { return d.value })
    
    return table;
    

    }

另外...我发现 d3.csv() 是异步的,因此如果您使用它来提取数据并生成表,请重构您的代码以考虑调用的异步性质。这可能意味着从 d3.csv() 调用内部处理所有逻辑

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

无法从网站检索HTML

JavaScript HTML集合显示为0长度

单击时如何检索HTML集合的索引?

如何解决 FileSystemException:无法检索文件长度,路径 ='...'

无法在Git Bash中检索MongoDB集合(Windows)

Flutter-无法检索Firebase集合中的文档列表

无法从Cloud Firestore检索Flutter中的多个集合

无法检索用户属性(联系人等集合)

无法使用javascript从html页面中的url检索JSON数据

Vue和Vuex无法在html中检索适当的值

file_get_contents无法从正确的URL检索html

$ _POST无法从html文件的文本字段中检索数据

无法从 JQuery 生成的 HTML 中检索数据属性

无法从 HTML 文本缓冲区检索 ITextDocument

无法从html表单传递的objectID中检索对象的属性

无法使用 Selenium (Python) 检索脚本执行的 html 源

数组返回0长度,无法在html中调用

检索对象集合

Mongodb嵌套集合检索

ef7无法检索子集合的子对象的属性

无法使用Flask从MySQL数据库将数据检索到html中

PHP / CURL / JSON / AJAX / HTML-无法检索我所需要的

PHP:RegEx 无法匹配使用 file_get_contents 检索的 HTML 页面

gae运行顺利,但无法使用html和client.js检索数据

无法使用Selenium和read_html从Macro Trends检索数据来创建数据框?

无法使用document.cookie(本地HTML文件)从先前的会话中检索Cookie

无法检索在 Localhost:port python 上运行的 HTML 表单数据

html上的django for循环无法检索其object_lists的每个数据字段

对齐html页面长度