[英]Why is my PHP code not working?
我已经写了一个代码-
<?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>
我想要的是,当我将鼠标悬停在带有theme_content
类的div
元素上时,其背景会根据动画函数中给定的背景而变化。 它适用于input
元素,但该脚本是原始javascript
,因此其中包含了php
,这就是为什么我在考虑使用的php
代码错误还是我的javascript
。 另外,当我将此代码包含在javascript
文件中时,它也可以正常工作。 通过将body
标签之间的script
标签包括在内,这是错误的吗? 请帮我解决这个问题。
提前致谢!
尝试将其放在脚本中。 <script></script>
而且也可能未加载DOM。 因此请尝试将wrappint放入$(document).ready(function() { /*Your code here*/})
并且还放置alert('hello');
检查该代码是否正在执行。 在调试中可能很有用。
最恰当的解释是您的URL请求中未将“主题”设置为“默认”。
这应该很容易检查。 您也可以在浏览器中“查看”->“页面源代码”,并查看您的php程序实际生成的html。 如果我的猜测是正确的,那么您将在页面中看不到鼠标悬停功能。
嗨,您应该这样写:-
<?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语句之间添加标签。
希望可以帮到您。
如果将type ='text / javascript'添加到脚本标签中,效果会更好吗?
<script type='text/javascript' src="domail_res/scripts/java/settings_form_css.php?theme=default"></script>
为什么不只将php逻辑放在您的主文件中,然后将javascript作为普通javascript包含在内?
<?php
if($domail_theme=='default')
{
?>
<script type='text/javascript' src="domail_res/scripts/java/settings_form_css.js"></script>
<?php
}
?>
似乎使事情变得过于复杂
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.