jQuery不适用于通过PHP回显的元素 [英] jQuery doesn't work on elements echoed via PHP

查看:66
本文介绍了jQuery不适用于通过PHP回显的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我所拥有的是一个从mysql数据库生成html表的PHP代码,然后我尝试使用一个使表可排序的jQuery插件。我有很多次这个问题,似乎无法在任何地方找到解决方案...为什么jQuery(或者是Javascript句点?)不能在PHP输出上工作?这不是解决方法吗?

What I have is a PHP code that generates a html table from mysql database, and then I'm trying to use a jQuery addon that makes the table sortable. I've had this problem many times and can't seem to find a solution anywhere... why doesn't jQuery (or is it Javascript period?) work on a PHP output? Isn't there a way around this?

以下是代码:

<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视图源中的end / script>标记处丢失了语法着色

UPDATE: For some reason syntax coloring is lost at the end / script> tag in chrome view source

推荐答案

Javascript是一种客户端技术,意思是让它使用服务器通过php直接生成的元素,有一些涉及体操。

Javascript is a client-side technology, meaning to get it work with elements generated directly by the server through php, there are some gymnastics involved.

尝试阅读 json_encode()函数和一般的ajax调用。

Try to read up on the json_encode() function in PHP and ajax calls in general.

此外,您可能对.delegate()jQuery方法感兴趣。

Also, you might be interested in the .delegate() jQuery method.

这篇关于jQuery不适用于通过PHP回显的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆