简体   繁体   English

如何在 php 中编写 html 代码?

[英]how to write html code inside php?

I want to write some html code in php.我想在 php 中写一些 html 代码。 In this html code I am calling a javascript function.在这个 html 代码中,我调用的是 javascript function。 but while it is calling the function there is a problem.但是在调用 function 时出现问题。 I think the problem is quotes but I couldn't fix it.我认为问题是引号,但我无法解决。

This is the html code that I want to put inside php.这是我想放入 php 中的 html 代码。

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

and this is my javascript code;这是我的 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>

How can i write this html inside php code?我如何在 php 代码中编写这个 html ?

Thanks谢谢

<?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 

you also could wrap your code in heredoc, and echo it afterwards http://www.php.net/manual/de/language.types.string.php#language.types.string.syntax.heredoc你也可以将你的代码包装在heredoc中,然后回显它http://www.php.net/manual/de/language.types.string.php#language.types.string.syntax.heredoc

you could echo the html;你可以呼应 html; eg例如

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

Just escape the double quotes with a backslash.只需用反斜杠转义双引号即可。

You can use PHP heredoc syntax:您可以使用 PHP heredoc 语法:

var $js = <<<JS变量 $js = <<<JS

// code // 代码

JS; JS;

Escapes and echo "<html_code>" is a noob style.转义和回显“<html_code>”是一种菜鸟风格。

Use scriptlets and the command echo.使用 scriptlet 和命令 echo。

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

and so far.到目前为止。 But pay attention of masking quotations signs and backslashes, etc.但要注意掩盖引号和反斜杠等。

Write html tags inside php code isn't nice.在 php 代码中写入 html 标签并不好。 Read about templates to php as Smarty以 Smarty 的身份阅读有关 php 的模板

http://www.smarty.net/ http://www.smarty.net/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM