如何将 JQuery 添加到 PHP 文件中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19616053/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add JQuery into PHP file
提问by Mamen
I have a problem to add JQuery in file this is my code:
我在文件中添加 JQuery 时遇到问题,这是我的代码:
<html>
<head>
<script src='http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js'>
</script>
<script>
$(document).ready(function(){
$('#k1').click(function(){
$('#k2').animate({marginLeft:'-50px'});
$('#k3').animate({marginLeft:'-450px'});
$('#k4').animate({marginLeft:'-450'});
});
});
</script>
</head>
<body>
<div class='wrapper'>
<div class='mainKotak'>
<div class='wKotak'>
<div class='kotak' id='k1' ></div>
</div>
</div>
</div>
</body>
</html>
the code above is in
上面的代码在
<?php
echo" ";
?>
the jquery isn't work , please someone help me because i'm newbie in jquery thanks
jquery 不起作用,请有人帮助我,因为我是 jquery 的新手,谢谢
SOLVED i'm just use jQuery with external javascript
已解决,我只是将 jQuery 与外部 javascript 一起使用
回答by Sanjay
don't use echo like that. You can write anything outside <?php ... ?>
, in simple HTML
不要像那样使用回声。你可以在外面写任何东西<?php ... ?>
,用简单的 HTML
回答by brandonscript
If I'm understanding you correctly, you need to echo out that jquery code via php? You can do this easily by escaping out any double quotes or backslashes (" , \) with a backslash. Since your code doesn't appear to have either, you should be fine.
如果我理解正确,您需要通过 php 回显该 jquery 代码吗?您可以通过使用反斜杠转义任何双引号或反斜杠 (" , \) 来轻松完成此操作。由于您的代码似乎没有,所以您应该没问题。
You may also find that because javascript is particular about line breaks sometimes, adding the white-space line break character to the end of your echo is beneficial: \n
您可能还会发现,由于 javascript 有时会特别关注换行符,因此在 echo 末尾添加空白换行符是有益的:\n
e.g.
例如
echo "<html>\n";
echo "<head>\n";
echo "<script src='http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js'>\n";
echo "</script>\n";
echo "<script>\n";
There is an online tool for easily wrapping raw HTML in php echo that may help you too: http://www.andrewdavidson.com/convert-html-to-php/
有一个在线工具可以轻松地将原始 HTML 包装在 php echo 中,也可以帮助您:http: //www.andrewdavidson.com/convert-html-to-php/
If it still doesn't work after doing that, you likely have a syntax error. Check the source of your php-generated code as well -- compare it to the code you've pasted above to ensure they match up.
如果这样做后它仍然不起作用,则您可能有语法错误。还要检查 php 生成的代码的来源——将它与上面粘贴的代码进行比较,以确保它们匹配。
@Sanjay's answer above also makes a lot more sense if you can do it that way instead. You can switch between php easily:
如果您可以这样做,上面@Sanjay 的回答也更有意义。您可以轻松地在 php 之间切换:
<?php
include"db.php";
if(!isset($_GET['page']))
{
header("location:index.php?page=home");
}
switch($_GET['page'])
{
case "home": $title = "TITLE WEB";
}
?>
<html>
<head>
<title><?php echo $titleVar; ?></title>
<link rel='stylesheet' href='css/style.css' type='text/css'>
<body>
...
<php echo "Other content"; ?>
</body>
</html>
回答by Mubin
You need to add quotes character (') on css attribute and write correctly
您需要在 css 属性上添加引号字符 (') 并正确写入
$('#k2').animate({marginLeft:'-50px'});
become
变得
$('#k2').animate({'margin-left':'-50px'});