0

我试图将类添加到一行$('tr:first-child').next().addClass("current");

整个 html

<!DOCTYPE html>
<html><head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>All Rows</title>

  <script type="text/javascript" src="jquery.js"></script>

  <style type="text/css">
    .container {padding-left: 20px; padding-right: 20px;}
.forms{
display:inline-block;
margin-top:2%;
}

.buttons{border:1px solid #708090;}
input {margin-bottom:2%;margin-right:5%;}

button{margin-right:3%;}

.forms{border-top:3px solid #708090;}
.tableholder{height:300px; overflow-y:auto;}
.current{background-color:pink;}
  </style>
<script type="text/javascript">
$(document).ready(function(){

   $.get("getall.php", function(data){       
    {
     $('.tableholder').append(data);
    } 
    });

$('tr:first-child').next().addClass("current");

$("#next").click(function(){
$("table").find('.current').removeClass("current");
});
});

</script>
</head>
<body>
<div id="sc" class="container">
<div class="tableholder">
</div>
</div>
</body></html>

和我的 php 脚本。不要担心已弃用的 mysql 函数。我们使用旧的 php 安装

<?php
  $host = "localhost";
  $user = "root";
  $pass = "";
  $databaseName = "caliban";
  $tableName = "caliban";
  $con = mysql_connect($host,$user,$pass);
  $dbs = mysql_select_db($databaseName, $con);
  $result = mysql_query("SELECT * FROM $tableName");            
  $array = mysql_fetch_assoc($result);                           

$result = mysql_query("SELECT * FROM $tableName");           

$rows = Array();
$i=0;
while($row = mysql_fetch_assoc($result)){
        //array_push($rows, $row);
      $rows[$i++] = $row;
}

echo "<table id='tab' border='1'>
<thead>
<tr>
<th><form><input type='checkbox' /></form></th>
<th>firstname</th>
<th>lastname</th>
<th>city</th>
<th>continent</th>
</tr>
</thead>
<tbody>";

for($j=0;$j<count($rows); $j++){
$id = $rows[$j]['id'];
$firstname = $rows[$j]['firstname'];
$lastname = $rows[$j]['lastname'];
$city = $rows[$j]['city'];
$continent = $rows[$j]['continent'];

      echo 
      "<tr id='$id' class=''>
<td><input type='checkbox' /></td>
<td>$firstname</td>
<td>$lastname</td>
<td>$city</td>
<td>$continent</td>
</tr>";
}

echo "</tbody>
</table>";
?>

获取成功,表格按原样显示,但无法应用课程。我哪里出错了?

4

3 回答 3

1

这是您的代码块。

$(document).ready(function(){

    $.get("getall.php", function(data){       
        {
            $('.tableholder').append(data);
        } 
    });

    $('tr:first-child').next().addClass("current");

调用是异步的$.get,这意味着脚本将在返回结果之前继续。因此,

$('tr:first-child').next().addClass("current");

之前被调用,

$('.tableholder').append(data);

试试这个:

$(document).ready(function(){

    $.get("getall.php", function(data){       
        {
            $('.tableholder').append(data);
            $('tr:first-child').next().addClass("current");
        } 
    });
于 2013-05-29T16:16:40.610 回答
1

您的.get()请求是异步的,因此当您尝试应用该样式时,还没有表格。追加数据后应用样式。

$.get("getall.php", function(data) {
    $('.tableholder').append(data);
    $('tr:first-child').next().addClass("current"); 
});
于 2013-05-29T16:17:37.140 回答
1

尝试这个:

$(document).ready(function () {

    $.get("getall.php", function (data) {

            $('.tableholder').append(data);
            $('tr:first-child').next().addClass("current");

    });



    $("#tab").on('click','#next',function () {
        $("table").find('.current').removeClass("current");
    });
});

顺便说一句,ID 在上下文页面上必须是唯一的,这意味着只有一个元素可以具有 ID 'next'

于 2013-05-29T16:18:20.400 回答