簡體   English   中英

jQuery不適用於通過PHP回顯的元素

[英]jQuery doesn't work on elements echoed via PHP

我所擁有的是一個PHP代碼,該代碼從mysql數據庫生成html表,然后我嘗試使用使該表可排序的jQuery插件。 我有很多次這個問題,似乎無法在任何地方找到解決方案...為什么jQuery(或者它是Javascript期?)不能在PHP輸出上工作? 有沒有辦法解決這個問題?

這是代碼:

<html><head>
<title>MySQL Table Viewer</title>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script src="http://tablesorter.com/jquery.tablesorter.min.js"></script>
<script src="http://tablesorter.com/addons/pager/jquery.tablesorter.pager.js"></script>
<script>
$(document).ready(function() 
{ 
    $("#mytable").tablesorter(); 
} 
); 
</script>
</head><body>
<?php
$db_host = 'localhost';
$db_user = 'root';
$db_pwd = 'lptm42b';

$database = 'sphinx';
$table = 'spheres';

if (!mysql_connect($db_host, $db_user, $db_pwd))
die("Can't connect to database");

if (!mysql_select_db($database))
die("Can't select database");

// sending query
$result = mysql_query("SELECT * FROM {$table}");
if (!$result) {
    die("Query to show fields from table failed");
}

$fields_num = mysql_num_fields($result);

echo "<h1>Table: {$table}</h1>";
echo "<table id=\"mytable\" border='1'><tr>";
// printing table headers
for($i=0; $i<$fields_num; $i++)
{
    $field = mysql_fetch_field($result);
    echo "<td>{$field->name}</td>";
}
echo "</tr>\n";
// printing table rows
while($row = mysql_fetch_row($result))
{
    echo "<tr>";

    // $row is array... foreach( .. ) puts every element
    // of $row to $cell variable
    foreach($row as $cell)
        echo "<td>$cell</td>";

    echo "</tr>\n";
}
mysql_free_result($result);
?></table>
</body>
</html>

輸出為:

<html><head>

<script src="../js/jquery_plugin.tablesorter.min.js"></script>

<script src="../js/jquery-1.6.min.js"></script>

<script src="../js/jquery_plugin.tablesorter.pager.js"></script>

<title>MySQL Table Viewer</title></head><body>

<h1>Table: lobby</h1><table id="mytable" border='1'><thead><tr><td>tableid</td>    <td>name</td><td>datecreated</td><td>active</td></tr></thead> 
<tbody><tr><td>12341231241</td><td>Oyunum1</td><td>2011-05-09 14:26:51</td><td>0</td>    </tr> 
<tr><td>6677768</td><td>m2</td><td>2011-05-05 14:26:39</td><td>1</td></tr> 
<tr><td>ddf1</td><td>m3</td><td>2011-05-09 14:27:19</td><td>0</td></tr> 
<tr><td>7856844444</td><td>m4</td><td>2011-05-09 14:27:31</td><td>0</td></tr> 
<tr><td>xxxxde4rfd</td><td>m5</td><td>2011-05-09 14:27:43</td><td>0</td></tr> 
</tbody></table>

</body></html>

<script>

$(document).ready(function() 

{ 

    $("#mytable").tablesorter(); 

} 

); 

</script>

更新:出於某種原因,在chrome視圖源中,語法着色在結尾/ script>標記中丟失

tablesorter插件在表中需要THEAD和TBODY標記才能起作用

您沒有給#mytable元素。

您需要使用與實際存在的元素匹配的選擇器。

編輯 :您的PHP有錯誤的引號,因此ID實際上不在字符串中。
查看頁面源。

您應該使用“ mytable”添加表的ID。

此外,還始終將thead,tbody放在表標簽中,以在使用JQuery時獲得更好的輸出。

遲到了這個聚會,但是您需要知道document.ready將不會拾取動態生成的html(如果該html是在最初加載dom之后生成的)。 有解決此問題的jQuery插件(liveQuery是其中的一種),但是Document.Ready事件在每次頁面加載時都會觸發一次,如果在觸發之后觸發了生成html的PHP,則您調用的tablesorter方法將無濟於事。分類。

Javascript是一種客戶端技術,意味着要使其與服務器通過php直接生成的元素一起使用,需要進行一些體操操練。

通常,嘗試閱讀PHP和ajax調用中的json_encode()函數。

另外,您可能對.delegate()jQuery方法感興趣。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM