简体   繁体   English

Javascript中的PHP回显

[英]PHP echo in Javascript

I have this code: 我有以下代码:

 document.getElementById('root').style.left = '<?php echo $page_position[$info["os"]][$info["browser"]][0]; ?>';
 document.getElementById('root').style.top = '<?php echo $page_position[$info["os"]][$info["browser"]][1]; ?>';

Why won't this work like this? 为什么这样不行呢?

Can anybody point me in the right direction? 有人能指出我正确的方向吗?

EDIT: 编辑:

<?php echo $page_position[$info["os"]][$info["browser"]][1]; ?>

echoed "top:300px;" 回显“ top:300px;”

Sorry guys, very stupid error of mine :/ 抱歉,我的愚蠢错误:/

如果将其放在.js文件中,则它将无法工作,因为它必须位于为PHP( .php )解析的页面中。

Things to check: 检查事项:

  1. Check to make sure those variables are available. 检查以确保这些变量可用。 print_r($info) will help you out. print_r($info)将帮助您。
  2. Make sure that the code is actually being filled with those variables (symptom of #1). 确保代码实际上被这些变量填充(#1的症状)。 View source and check that they are there. 查看源代码并检查它们是否在那里。
  3. Ensure you don't have any JavaScript errors preceding your code. 确保在代码之前没有任何JavaScript错误。 Open your console (like Firebug) and check to make sure you don't. 打开您的控制台(如Firebug),然后检查以确保您没有。
  4. Ensure that a root element exists. 确保root元素存在。 Again, use your console. 再次使用控制台。

My money goes on #3, you probably have a JavaScript error somewhere that is stopping execution of the script. 我的钱花在了#3上,您可能在某个JavaScript错误中停止了脚本的执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM