提问者:小点点

在javascipt脚本中分配PHP变量


我正在开发一个内置在jquery和PHP中的遗留应用程序。我不确定如何正确赋值变量。当视图源时,PHP部分为空。

if(window.location.href.indexOd('age') >= 0){
  <?php
      $width = '750px'
  ?>
} else {
    $width = '500px'
}

然后使用jquery分配width变量

$('#main-dev').attr('style', max-width:<?php echo $width;?>");

这没有应用正确的宽度。

有什么想法为什么php变量不采取吗?

上面的JS出现在源代码中,如

if(window.location.href.indexOd('age') >= 0){
} else {
}

我以前从来没有使用过PHP,只是想在这个遗留站点上工作?


共1个答案

匿名用户

在JS全局范围中创建一个var“width”并更改值。注意,正确的是“indexof”。

var width = '500px';
if(window.location.href.indexOf('age') >= 0){
    width = '750px';
}

因此,使用“width”var。

$('#main-dev').attr('style', 'max-width:' + width);