当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - PHP/Jquery - Echoing jquery in a PHP function

问题描述:

I have the following function:

function loader($time,$page){

echo "<script type='text/javascript'>

$( document ).ready(function() {

$('.register-container').hide();

$('.top-color').animate({height:'100%'}, 500);

window.setTimeout(

function() {

$('.loading-overlay').show();

},

600

);

//Simulate loading... and then redirect.

window.setTimeout(

function() {

$('.loading-overlay').hide();

$('.top-color').animate({height:'0px'}, 500);

},

2600

);

});

</script>";

echo '<div class="loading-overlay" >

<div class="loading-circles">

<!-- MAINTAINS CONSISTANT COLOR CENTER -->

<div class="circle hold" ></div>

<!-- ITERATION POINT FOR ANIMATION -->

<div class="circle first"></div>

<div class="circle"></div>

<div class="circle"></div>

</div>

<!-- ICON -->

<div class="name"><img src="images/loader/loadertext-green.png" /></div>

</div>';

echo "<meta http-equiv=refresh content=$time;URL='$page'>";

}

Which should add the above jQuery code to the page, and execute it.

Now my problem is, that when the function is called in the page, like below:

<?php

if($checksuccess){ loader("4","/account"); }

?>

I get the following jQuery error:

Uncaught ReferenceError: $ is not defined

What am I doing wrong?

网友答案:

Php is expecting a variable name after the $ sign because you are using double quotes.

Try changes the single quotes with double quotes inside the echo and sorround it with single quotes. The opposite that you have.

网友答案:

Your second echo block starts and ends with a single quote, it should be a double quote. Also, the string inside of an echo must escape double quotes. You can generally use single quotes in html, but it is probably better to escape the double quotes.

echo "<div class=\"loading-overlay\" > <div class=\"loading-circles\"> Use escape \" on html attributes when echoing from PHP </div></div>";

分享给朋友:
您可能感兴趣的文章:
随机阅读: