簡體   English   中英

如何在 php 中編寫 html 代碼?

[英]how to write html code inside php?

我想在 php 中寫一些 html 代碼。 在這個 html 代碼中,我調用的是 javascript function。 但是在調用 function 時出現問題。 我認為問題是引號,但我無法解決。

這是我想放入 php 中的 html 代碼。

<table>
 <tr>
 <a href="javascript:chng('img');"><img src="s.png" name="img"></a> 
 </tr>
</table>

這是我的 javascript 代碼;

<script type="text/javascript">
img1 = "s.png";
img2 = "k.png";
function chng(c_img) {
if (document[c_img].src.indexOf(img1)!= -1) document[c_img].src = img2;
else document[c_img].src = img1;
} 

</script>

我如何在 php 代碼中編寫這個 html ?

謝謝

<?php

// your php code

?>

<table>
 <tr>
 <a href="javascript:chng('img');"><img src="s.png" name="img"></a> 
 </tr>
</table>

<?php

// your php code

?>

<script type="text/javascript">
img1 = "s.png";
img2 = "k.png";
function chng(c_img) {
if (document[c_img].src.indexOf(img1)!= -1) document[c_img].src = img2;
else document[c_img].src = img1;
} 

</script>

<?php 

你也可以將你的代碼包裝在heredoc中,然后回顯它http://www.php.net/manual/de/language.types.string.php#language.types.string.syntax.heredoc

你可以呼應 html; 例如

echo "<table>
 <tr>
 <a href=\"javascript:chng('img');\"><img src=\"s.png\" name=\"img\"></a> 
 </tr>
</table>
";

只需用反斜杠轉義雙引號即可。

您可以使用 PHP heredoc 語法:

變量 $js = <<<JS

// 代碼

JS;

轉義和回顯“<html_code>”是一種菜鳥風格。

使用 scriptlet 和命令 echo。

<?
  echo "<script type=\"text\/javascript\">"
?>

到目前為止。 但要注意掩蓋引號和反斜杠等。

在 php 代碼中寫入 html 標簽並不好。 以 Smarty 的身份閱讀有關 php 的模板

http://www.smarty.net/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM