沒有人知道一種方式來獲得PHP與jQuery的附加()工作? 說我們有PHP的jQuery的附加
$('.class0').click(function(){
$('.class1').append('<?=PHPCODE?>');
});
我們如何讓php在追加後工作? 謝謝!
沒有人知道一種方式來獲得PHP與jQuery的附加()工作? 說我們有PHP的jQuery的附加
$('.class0').click(function(){
$('.class1').append('<?=PHPCODE?>');
});
我們如何讓php在追加後工作? 謝謝!
試試這個:
HTML文件(的index.php)
<?php
include('config.php'); // Example
$foo = "<span style='color:red;'>WTF</span>" //OR <span style="color:red;">WTF</span>
?>
<html>
<head>
<script type="text/javascript">
$.ready(function()
{
$('.element').click(function()
{
$(this).append('<?php echo str_replace("'","\'",$foo);?>');
return false;
});
});
</script>
</head>
<body>
<a href="#" class="element">Element</a>
</body>
</html>
- 使用
<?php echo $foo; ?>
,<?=$foo;?>
不被recomendated。- 轉義特殊字符,例如:'(簡單的)。
- 使用Smarty的模板PHP框架
Smarty的模板PHP框架
vista.php(MVC OOP)
<?php class myClass { public function myMethodName() { $var = '<span>text</span>'; $this->template->assign('foo' , $var); $this->template->display('myTemplate.html'); } } ?>
vista.php(NO OOP)
<?php $var = '<span>text</span>'; $smarty = new Smarty; $smarty->assign('foo' , $var); $smarty->display('myTemplate.html'); ?>
myTemplate.html
<html> <head> <title>{$title}</title> </head> <body> {if $text} {$text} <ul> {foreach from=$list item=li} <li>{$li}</li> {/foreach} </ul> {/if} </body> </html>
jquery是javascript,它在瀏覽器上執行,而php是服務器端腳本,所以理論上PHP腳本總是先運行在任何JavaScript之前。如果您希望在頁面加載完成後運行任何PHP腳本(例如,當用戶單擊某個按鈕時),則需要執行AJAX調用。 ($獲得(),$ .load()等都是使用jQuery的例子)
在更多的細節,你editted問題: 你的代碼,你寫內部追加將呈現爲純文本的PHP腳本。相反,在append之前執行ajax調用,並將ajax調用的結果存儲在變量中。然後使用:append(variable)。
您可能也有興趣做一些DOM操作,如果您從php腳本中加載大量文本,效率更高。您可以從php腳本返回JSON對象,然後使用附加組合來創建所需的結果。這減少了需要傳輸的數據量,但JavaScript代碼可能很長。
PHP生成靜態HTMLS和jQuery處理靜態HTMLS,因此,它不是同樣的事情 – 2012-01-06 14:42:32
追加後?你是什麼意思?你想追加由PHP生成的數據? – devdRew 2012-01-06 14:42:33
你的意思是這樣的? '$('。class0')。click(function(){$('。class1')。append('<?php echo'foobar';?>');});' – giorgio 2012-01-06 14:44:59