由于 php 包含和外部文件导入,Jquery 不再工作
我注意到自从我开始做一些 php include 以来我的 jquery 代码不再工作了。
显然,我希望从index.php主页的js文件导入我的代码。但是,我的 html 代码将从 php include 生成(我不知道这是否是一个问题)。
无论如何,这是我的 php 页面(index.php)的结构:
[...]
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript" src="js/comments.js"></script>
</head>
<body>
<?php
// HEADER -----------------------------------------------
include("front/index/header.php");
// ------------------------------------------------------
?>
<div class="containerMiddleFullSize">
<div class="containerMiddle">
<?php
// BODY -------------------------------------------------
if ($_GET['page'] == "home")
{
include("front/index/home.php");
}
[...]
这是我的 jquery 代码(js/comments.js):
$(function()
{
$(".content").Watermark("Écrire un commentaire...");
$(".content").focus(function()
{
$(this).height("50px"); //animate({"height": "50px",}, "fast" );
$(this).next(".button_block").slideDown("fast");
return false;
});
$(".cancel").click(function()
$(this).parent(".button_block").slideUp("fast").prev(".content").height("18px");
return false;
//}
});
});
欢迎任何帮助;)
干杯,
Gotye。
I noticed my jquery code is not working anymore since I started doing some php includes.
Obviously, I would like my code to be imported from a js file from the index.php main page. However, my html code will be generated from php includes (i dunno if this could be a problem).
Anyway, here is the structure of my php page (index.php) :
[...]
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript" src="js/comments.js"></script>
</head>
<body>
<?php
// HEADER -----------------------------------------------
include("front/index/header.php");
// ------------------------------------------------------
?>
<div class="containerMiddleFullSize">
<div class="containerMiddle">
<?php
// BODY -------------------------------------------------
if ($_GET['page'] == "home")
{
include("front/index/home.php");
}
[...]
Here is my jquery code (js/comments.js) :
$(function()
{
$(".content").Watermark("Écrire un commentaire...");
$(".content").focus(function()
{
$(this).height("50px"); //animate({"height": "50px",}, "fast" );
$(this).next(".button_block").slideDown("fast");
return false;
});
$(".cancel").click(function()
$(this).parent(".button_block").slideUp("fast").prev(".content").height("18px");
return false;
//}
});
});
Any help welcome ;)
Cheers,
Gotye.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试更改定义以使用 live 函数,例如
我猜测在调用 JS 时,相关元素不存在,因此没有任何内容与它们绑定。使用 live 可确保任何具有指定特征的未来元素都将被正确绑定。
Try changing the definitions to use the live function e.g.
I'm guessing that at the time your JS is called, the elements in question don't exist therefore nothing is bound to them. Using live ensures that any future elements with the specified characteristics, will be bound correctly.
正如您所说,只要您不包含 PHP 文件,您的 Javascript 就可以正常工作。
但是,一旦你将这些包括在内,它就会停止工作。
因此,请确认您没有任何未封闭的 HTML 标记。有时,不完整的标签会给 JS 的运行带来问题。
谢谢!
侯赛因
As you said that your Javascript is working fine as long as you don't include your PHP files.
But, it stops working once you make those include.
So, Please verify that you don't have any unclosed HTML tags. Sometimes, incomplete tags creates the problem for JS to work.
Thanks!
Hussain
也许您剪切并粘贴了这个错误,但也许为 onclick 函数添加了一个开括号?
更改:
至:
maybe you cut and pasted this wrong but maybe add in an open bracket for the onclick function?
change:
to: