1 回答

TA貢獻1856條經驗 獲得超11個贊
如果$c
是你的 php 變量,你需要引用它并將其加入echo
標簽內。
如果它是一個integer
,這應該可以...
<?php
echo "<script type='text/javascript' src='https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js'></script>
? ? ? <script type='text/javascript'>
? ? ? ? ? $(document).ready(function() {
? ? ? ? ? var count = ($('.data').length + 25);
? ? ? ? ? var c = " . $c . ";
? ? ? ? ? $('#data').load('data.php')
? ? ? ? ? ? ?$('.btn-data').click(function() {
? ? ? ? ? ? ? ? ?count = count + 25;
? ? ? ? ? ? ? ? ? $('#data').load('data.php?v=' + count + '&c=' + c)
? ? ? ? ? ? ?})
? ? ? ? ? });
? ? ? </script>";
如果這是一個,string這應該可以工作......
<?php
echo "<script type='text/javascript' src='https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js'></script>
? ? ? <script type='text/javascript'>
? ? ? ? ? $(document).ready(function() {
? ? ? ? ? var count = ($('.data').length + 25);
? ? ? ? ? var c = '" . $c . "';
? ? ? ? ? $('#data').load('data.php')
? ? ? ? ? ? ?$('.btn-data').click(function() {
? ? ? ? ? ? ? ? ?count = count + 25;
? ? ? ? ? ? ? ? ? $('#data').load('data.php?v=' + count + '&c=' + c)
? ? ? ? ? ? ?})
? ? ? ? ? });
? ? ? </script>";
如果它是一個object,您將必須解析 var 或對其進行編碼。
有時,如果您想在 php 中回顯或返回大塊 html,可以使用輸出緩沖區來捕獲 html,然后您可以通過函數返回 html,或回顯它。當你的 html 沒有包含在 echo 引號標簽內時,這只會讓你的 html 更容易閱讀。
<?php
// c sting php var?
$c = 'string';
// start output buffer
ob_start();
?>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script type="text/javascript">
? ? $(document).ready(function() {
? ? ? ? let count = ($('.data').length + 25);
? ? ? ? let c = '<?=$c?>';
? ? ? ? $('#data').load('data.php');
? ? ? ? $(this).on('click', '.btn-data', function() {
? ? ? ? ? ? count = count + 25;
? ? ? ? ? ? $('#data').load('data.php?v=' + count + '&c=' + c);
? ? ? ? });
? ? });
</script>
<?php
// store everything between ob_start() and here into a php variable
$script = ob_get_clean();
// return script html
return $script;
// or echo script html
echo $script;
// not both together obviously
?>
添加回答
舉報