如何在一个变量中编写多行jquery代码

时间:2014-10-23 06:46:40

标签: php jquery

我有mainpage.php和function.php。

function.php

<?php
function get_header($js){
echo <<END
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
<meta http-equiv="Content-Type" content="application/vnd.wap.xhtml+xml; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="mycss.css?v=<?php echo time(); ?>" />
<script type="text/javascript" src="jquery1.9.1.js"></script>
<script type="text/javascript">.$js.</javascript>
</head>
END;
}
?>

该function.php将包含在每个php页面上,例如main.php。

main.php

<?php
include('function.php');
$js = 
$(document).ready(function(){
$("a.div").click(function(){
   $(".active").removeClass("active");
   $(this).addClass("active");
   $(".tcont").hide();
   var ptype= $(this).attr("title");
   $("#"+ptype).show();
 });
});

get_header($js);
?>

由于每个页面的javascript代码可能不同,所以我想在每个页面的变量中写下javascript代码,以便可以从get_header($ js)函数调用该变量。但我不知道在一个变量中引用/编写javascript代码的正确方法。

我不想将javascript代码放在单独的.js文件中,稍后将其包含在function.php页面中。

我的问题是,如何在一个变量中编写多行javascript代码?

1 个答案:

答案 0 :(得分:2)

您可以将它们用作字符串:

<?php
include('function.php');
$js = '
$(document).ready(function(){
$("a.div").click(function(){
   $(".active").removeClass("active");
   $(this).addClass("active");
   $(".tcont").hide();
   var ptype= $(this).attr("title");
   $("#"+ptype).show();
 });
});
';
get_header($js);
?>

如果你有逃避的东西,你可以使用\&#39;

相关问题