【问题标题】:get id of a cell in a table inside a div (JS and HTML)获取div内表格中单元格的ID(JS和HTML)
【发布时间】:2009-10-24 22:20:23
【问题描述】:

您好,我有一个 php 脚本,可以根据特定条件绘制表格。绘制表格时,它位于一个名为“window”的 div 中,该表格名为“visi”,然后我想使用下面的代码来获取表格中每个单元格的 id。我可以毫无问题地获得类名,但从 id 中绝对得不到任何东西。谁能告诉我我做错了什么?我在不在 div 内的表上尝试了类似的代码,它工作正常。任何帮助都会很棒,我希望代码有意义。

function confirm_allocate() {
var msg = ""   
var tab = document.getElementById('visi');
var r = tab.rows.length

for (i=0; i<r; i++){
cc = tab.rows.cells.length
         for (col=0; col<cc; col++){
            x=document.getElementById('visi').rows.cells;   

            iden = x[col].className
            ref =  x[col].id

            msg += "Class =" + iden + " ///    Id =" + ref +  "\r" 
         } 
} 
alert (msg ) 
}

如果有帮助,这是绘制表格的代码(但在获取表格信息后使用 js/ajax 调用)

<?php

$table = ""  ;
include '../database_sql/dbconnect.php' ;
include '../functions/job_details.php';
include '../functions/check_date.php';
include '../functions/stock_list.php' ; 
include '../functions/allocated_on_date.php' ; 

$jobdate = $_GET['jobdate'] ;
$jobnumber = $_GET['jobnumber'] ;
$jobname = $_GET['jobname'] ;

$screens = screens_per_job($jobnumber,$size) ;

$table =  "<h2 align= 'center' > $jobname (Job Number $jobnumber) : Screens required : $screens </h2>"  ;
$table .= "<table id='visi' width='480px'  align='center'  cellspacing='1'  cellpadding='1'   border='1' ><tr>"  ;

// get stock list from DB 
stock_list() ; 
$len = count( $stock);

$evresult = mysql_query("SELECT * FROM event WHERE jobnumber = '$jobnumber' ")  ;
$event_row = mysql_fetch_array($evresult);

for ($counter = 0; $counter < $len; $counter++) {
      $item =  $stock[$counter] ;
      $items = $item . "s" ; 
      $booked_for_job = $event_row[$items] ;
      $result = mysql_query("SELECT * FROM $item ")  ;
      allocated_on_date($jobdate) ; // function 

         if ($booked_for_job) {   
         $count =  1 ;
         $table .= "<td >$item<br> [$booked_for_job to Allocate] </td> " ;

               WHILE ($row = mysql_fetch_array($result)) { ;
               $booked_job = $screens[$item][$count]["job"] ;  // from the   allocated_on_date($jobdate)  function  
               $description = $row['trailer_id'];
               $class = $items ;
               $id_items = $items . $count ;
               $count ++ ;   

                        if ($booked_job == $jobnumber) {  // allocated to current job
                        $table .= "<td class='truckbox' > <div class='$class' id='$id_items' onClick='allocate(\"$booked_for_job\",\"$id_items\")'  > " ;
                        $table .= "$num </div>   </td>"  ;

                                 } ELSEIF ($booked_job === 0 ) {  // available to allocated 
                                 $class .= "g"   ;
                                    $table .= "<td class='truckbox' > <div class='$class' id='$id_items' onClick='allocate(\"$booked_for_job\",\"$id_items\")'  > " ;
                                 $table .= "$num </div>   </td>"  ;

                                                   } ELSE {    // allocated to ANOTHER job 
                                                   $class .= "a"   ;
                                                   $table .= "<td class='truckbox' > <div class='$class' id='$items' > " ;
                                                   $table .= "</td> "  ;
                                                   }

               } // while 

         $table .= "</tr>" ; 
         } ; // if
}; //for


$table .= "</table> " ;

$table .= "<table width='200px'  align='center'  cellspacing='12'  cellpadding='1'   ><tr>" ; // draw table buttons close and allocate
$table .= "</tr><tr>" ;
$table .= "<td class='truckbox' <div class='yesbutton' id='yes' onClick='confirm_allocate()' ; return false ;  >  ";
$table .= "<td class='truckbox' <div class='nobutton' id='no' onClick='hide()'   >  ";
$table .= "</tr></table> ";

echo $table ; // finally draw table   

include '../database_sql/dbclose.php' ;

?>

【问题讨论】:

  • 我认为您需要再次尝试代码突出显示。确保每行代码至少缩进 4 个空格或 1 个制表符...
  • 是否可以显示生成的表格? (HTML)
  • 罗伯特,这里是表格的链接abbeysoft.co.uk/adi/diary_booking/diary_view.php 从那里您需要点击“列表日期范围”并确保该范围包括 2009-10-15,然后点击橙色的 Pontefract Racecourse 这将显示表格。如果您检查元素,您可以清楚地看到每个单元格都有一个唯一的 ID 谢谢...

标签: javascript html ajax


【解决方案1】:

我在正在生成的表格单元格(td 元素)上看不到任何 id 属性。所以ref 只是空白或未定义。

如果这不是问题,那么它可能是.rows.cells。请改用.getElementsByTagName('td')。我认为那条线应该在循环之外,因为它在每次迭代中都不会改变。

另外,您的 HTML 的后半部分是错误的 - 您没有关闭 td 标记。而不是&lt;td class='truckbox' &lt;div class... 应该是&lt;td class='truckbox'&gt;&lt;div class...

【讨论】:

  • 我向 DG 道歉! ID 在 div 元素内。一旦我将其移至 TD,一切正常。非常感谢您的帮助
【解决方案2】:

您忘记访问数组元素:

...
for (i=0; i<r; i++) {
  var row = tab.rows[i];
  var cc = row.cells.length;

  for (col=0; col<cc; col++){
    var x = row.cells[col];
...

【讨论】:

  • 谢谢,但无论你的方式还是我的方式都行不通,干杯
  • 您的 和
    元素在您链接的示例页面上似乎都没有 id 属性。
猜你喜欢
  • 2015-05-22
  • 2012-10-02
  • 2015-12-05
  • 2014-07-30
  • 2014-07-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-18
相关资源
最近更新 更多