【发布时间】:2015-09-11 22:47:51
【问题描述】:
<script type="text/javascript">
function run()
{
var paper = Raphael( $('.wrapper')[0], 600, 600 ),
path = paper.path( Raphael.transformPath(pdefs[useDef].path, pdefs[useDef].transform) )
.attr( 'stroke-width', 10 )
.attr( 'stroke', 'rgb(80,80,80)' ),
$shim = $('<div id=\'shim-1\'><img src=images/buttons/photo.png></div>') //How to use php assign this line?
}
$(function() {
run();
});
</script>
我想用php在上面写一行代码:$shim = $('<div id=\'shim-1\'><img src=images/buttons/photo.png width=75px height=75px></div>')。我可以编写如下代码吗? :
<script type="text/javascript">
function run()
{
var paper = Raphael( $('.wrapper')[0], 600, 600 ),
path = paper.path( Raphael.transformPath(pdefs[useDef].path, pdefs[useDef].transform) )
.attr( 'stroke-width', 10 )
.attr( 'stroke', 'rgb(80,80,80)' ),
</script>
<?php
Use mysql get data from database...
if (condition) {
?>
<script type="text/javascript"> $shim = $('<div id=\'shim-1\'><img src=images/buttons/photo.png width=75px height=75px></div>') </script>
<?php
}
?>
<script type="text/javascript">
}
$(function() {
run();
});
</script>
但我认为页面会先加载php,这意味着$shim将首先分配,然后才开始执行javascriptfunction run()。这样,$shim就会在javascriptfunction run()之外,如何使用php将$shim赋值给javascriptfunction run()呢?
我发现了实际情况。问题实际上是我在启动 php 标签<?php 之前关闭了javascript 标签</script>。其实我可以直接启动 php 标签而不用关闭 javascript 标签。例如,<script type="text/javascript"> Java codes here... <?php php codes here.... ?>
<script type="text/javascript">
function run()
{
var paper = Raphael( $('.wrapper')[0], 600, 600 ),
path = paper.path( Raphael.transformPath(pdefs[useDef].path, pdefs[useDef].transform) )
.attr( 'stroke-width', 10 )
.attr( 'stroke', 'rgb(80,80,80)' ),
<?php
Use mysql get data from database...
if (condition) {
$shim = $('<div id=\'shim-1\'><img src=images/buttons/photo.png width=75px height=75px></div>')
<?php
}
?>
}
$(function() {
run();
});
</script>
实际上如何调用这个问题?我认为这个问题与javascript之前的php加载无关......
【问题讨论】:
-
PHP 在网络浏览器获取页面源代码之前运行。因此,这个问题中的 PHP 是没有根据的;只有 HTML(带有 JS)。要推迟 PHP 本身(如果确实是“实际问题”),它必须在 单独 请求中处理 - XHR、带参数的回发等。
-
大胆猜测:您是否正在寻找发送 AJAX 请求?即 PHP 加载页面,发送到浏览器 > javascript 在浏览器上执行 > 向服务器 (PHP) 发送 ajax 请求 > 将数据发送回 javascript,您可以随意使用它。
标签: javascript php jquery mysql raphael