在JQuery中使用PHP变量 [英] Use a PHP variable in JQuery

查看:68
本文介绍了在JQuery中使用PHP变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

反正我可以在JQuery脚本中使用php变量吗?

Is there anyway I can use a php variable in the JQuery script?

示例:

  • PHP变量:$sr2
  • JQuery脚本的摘要(带有变量):$('#a2_bottom_$sr2')
  • PHP variable: $sr2
  • Excerpt of JQuery script (with variable): $('#a2_bottom_$sr2')

如何使该变量在该JQuery部分中有效?

How can I make it so the variable is valid in that JQuery part?

谢谢

推荐答案

您可以简单地使用PHP回显代码以启动JavaScript变量.

What you could simply do is use your PHP to echo out the code to initiate a JavaScript variable.

<script type="text/javascript">
<?php

  $phpVar = "foo";
  echo "var phpVariable = '{$phpVar}';";

?>
</script>

一旦解析了PHP代码,并将HTML发送给用户-他们将看到的只是PHP echo的结果-

Once the PHP code is parsed, and the HTML is sent to the user - all they will see is the result of the PHP echo -

<script type="text/javascript">
  var phpVariable = 'foo';
</script>

现在您的phpVariable可用于您的JavaScript!因此,您可以像在其他任何情况下一样使用它-

Now your phpVariable is available to your JavaScript! So you use it like you would in any other case -

$("div."+phpVariable);

这将检索具有foo类的任何<div>元素-

That will retrieve us any <div> element with a foo class -

<div class="foo"></div>

这篇关于在JQuery中使用PHP变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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