如何从PHP调用jQuery函数?
<?php
if ($error == 1) {
?>
<script type="text/javascript">
error('1');
</script>
<?
}
?>
它不起作用。
答案 0 :(得分:4)
不,你不能从PHP调用jQuery函数。但是你可以使用PHP生成html,它将在运行时执行jQuery函数。
那个很大的区别
答案 1 :(得分:4)
脚本标签不能直接注入php标签....但如果你回应它,脚本将工作....以下代码工作:
<?php
if( some condition ){
echo "<script>alert('Hello World');</script>";
}
?>
如果使用jQuery,则必须将代码包装在:
中jQuery(function(){
//Your Code
});
然后回应它......
答案 2 :(得分:3)
这取决于您将此代码片段放在HTML输出中的位置。
我认为其中一个解决方案应该有效:
<?php
if ($error == 1) {
?>
<script type="text/javascript">
$(function() {
error('1');
})
</script>
<?
}
?>
<?php
if ($error == 1) {
?>
<script type="text/javascript">
$(document).ready(
function() {
error('1');
}
)
</script>
<?
}
?>
答案 3 :(得分:0)
您需要指定error('1')
是对jQuery函数的调用:
jQuery.error('1');
// or, uning the $ alias:
$.error('1');
答案 4 :(得分:0)
<?php
if(this == that) {
//do something in php
} else {
//do something with jquery (i.e. fade something in/out
}
&GT;