0

PHPドキュメントの最後のフッターに変数があります。ただし、これはテンプレートであるため、変数のコンテンツは各ページで異なる方法で作成されます。その変数のほとんどはJSであり、次のようになります。

  $myVar = '
    $(function() {
    '.$qTip.'

    $(".delDupe").click(function(){
        $(this).parent().find("input").val("");
        $(this).remove();
    });

    function custAxis() {
        if ($("#axisChk").is(":checked")){
            $(".customAxis").show();
        } else {
            $(".customAxis").hide();
        }
    }

    custAxis();
 });

これは、すべてのJSのほんの一部です。このJSを含めたいのですが、それでもPHP変数の一部として、ただしPHPの外部に保持します。出来ますか?

$myVar = '?>
      // my JS
<? ';
4

4 に答える 4

2

次の形式を使用できます。

$myVar = <<<EOD
Example of string
spanning multiple lines
using heredoc syntax.
EOD;
于 2012-04-19T01:30:28.517 に答える
1

ヒアドキュメントを使用できます

<?php 
$myVar = <<<EOD
   $(".delDupe").click(function(){
        $(this).parent().find("input").val("");
        $(this).remove();
    });

    $qTip

    function custAxis() {
        if ($("#axisChk").is(":checked")){
            $(".customAxis").show();
        } else {
            $(".customAxis").hide();
        }
    }
EOD;
?>

または、ob_startを使用してPHPから抜け出し、出力を変数として取得することもできます。これが、すべてのビュー/htmlをロードする方法です。

<?php
ob_start();
?>
  $(".delDupe").click(function(){
        $(this).parent().find("input").val("");
        $(this).remove();
    });

    <?=$qTip;?>

    function custAxis() {
        if ($("#axisChk").is(":checked")){
            $(".customAxis").show();
        } else {
            $(".customAxis").hide();
        }
    }
<?php
$myVar = ob_get_contents();
ob_end_clean();
echo $myVar;
?>
于 2012-04-19T01:39:02.007 に答える
1

ヒアドキュメントを使用できます:

<?
$myVar = <<<END
$(function() {
....    
END;

echo $myVar;
?>
于 2012-04-19T01:31:51.473 に答える
0

JavaScriptをhtmlに入れ、phpからその$qTip文字列を出力します。それはあなたがしたものと多かれ少なかれ同じ効果ですが、別の方法で書かれています。

$(function() {
<?=$qTip?>

$(".delDupe").click(function(){
    $(this).parent().find("input").val("");
    $(this).remove();
});

function custAxis() {
    if ($("#axisChk").is(":checked")){
        $(".customAxis").show();
    } else {
        $(".customAxis").hide();
    }
}

custAxis();
});
于 2012-04-19T01:35:21.633 に答える