j查询以显示文本


jQuery to display text

我使用以下JavaScript在我的页面上以<div id="st-class">显示文本:

<script type="text/javascript">
$(function(){
$("#st-class").html("class 1");
});

但是现在我想显示一个来自我的数据库的 PHP 值:

$res_class = mysql_result($result,$j,'class');

这个 JavaScript 不起作用:

$(function(){
$("#st-class").html($res_class);
});

有人可以告诉我该怎么做吗?

你需要

这样:

$(function(){
  $("#st-class").html(<?php echo $res_class; ?>);
});

将此行保留在 .php 文件中,而不是 js 文件中

同时以内联方式打印值。不确定 php 语法。但我们确实喜欢下面的 ASP.Net

$(function(){
$("#st-class").html(<%= res_class %>);
});

从佩德罗斯回答,php语法

$(function(){
$("#st-class").html(<?php echo $res_class; ?>);
});

尝试

$(function(){
  $("#st-class").html(<?php echo json_encode($res_class); ?>);
});

不要忘记引号。

$(function(){
  $("#st-class").html("<?php echo $res_class; ?>");
});