为什么我的PHP代码不起作用?

时间:2011-03-29 04:51:55

标签: php jquery html jquery-ui

我已经制作了一个代码 -

<?php

header("Content-type: text/javascript");
require_once('../php/config.php');
$domail_theme = $_GET['theme'];

?>

<?php
  if($domail_theme=='default')
  {
?>

  $('div.theme_content').mouseover(function () {
    $(this).stop().animate({
      backgroundColor : '#234'
    }, 250, 'linear', function() { });
  });

  $('div.').mouseout(function () {
    $(this).animate({
      backgroundColor : '#ddd'
    }, 400, 'linear', function() { });
  });

<?php
  }
?>

这是包含标签的脚本 -

<script src="domail_res/scripts/java/settings_form_css.php?theme=default"></script>

我想要的是当我将div元素与类theme_content鼠标悬停在一起时,它的背景会根据animate函数中的给定值进行更改。它适用于input元素,但该脚本是原始的javascript,在此我已包含php这就是为什么我在想我使用的php代码是错误的或我的javascript。此外,当我在javascript文件中包含此代码时,它可以正常工作。我在script标记之间添加了body标记,是不是错了?请帮我解决这个问题。

提前致谢!

5 个答案:

答案 0 :(得分:1)

尝试将其放入脚本中。 <script></script>

并且DOM可能没有被加载。所以尝试在$(document).ready(function() { /*Your code here*/})

中加入包装

还要alert('hello');检查代码是否正在执行。它可能在调试时很有用。

答案 1 :(得分:0)

最可能的解释是,您的网址请求中的“主题”未设置为“默认”。

这应该很容易检查。你也可以“查看” - &gt;浏览器中的“页面源”并查看您的php程序实际生成的html。如果我的猜测正确,您将无法在页面中看到鼠标悬停功能。

答案 2 :(得分:0)

你应该这样写: -

<?php
 if($domail_theme=='default')
{
?>
<script>

$('div.theme_content').mouseover(function () {
$(this).stop().animate({
  backgroundColor : '#234'
}, 250, 'linear', function() { });
});

$('div.').mouseout(function () {
$(this).animate({
  backgroundColor : '#ddd'
}, 400, 'linear', function() { });
});
</script>
<?php
}
?>

只需要在php if语句之间添加标记。

希望可以帮助你。

答案 3 :(得分:0)

如果在脚本标记中添加type ='text / javascript',它会更好吗?

<script type='text/javascript' src="domail_res/scripts/java/settings_form_css.php?theme=default"></script>

答案 4 :(得分:0)

为什么不将php逻辑放在主文件中并将javascript包含为普通的javascript?

<?php
 if($domail_theme=='default')
{
?>
<script type='text/javascript' src="domail_res/scripts/java/settings_form_css.js"></script>
<?php
}
?>

似乎过于复杂的事情

相关问题