javascript 如何在php中编写html代码?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5804055/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to write html code inside php?
提问by omer
I want to write some html code in php. In this html code I am calling a javascript function. but while it is calling the function there is a problem. I think the problem is quotes but I couldn't fix it.
我想用php写一些html代码。在这个 html 代码中,我调用了一个 javascript 函数。但是在调用该函数时出现了问题。我认为问题出在引号上,但我无法解决。
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
谢谢
采纳答案by Luceos
you could echo the html; eg
你可以回显html;例如
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.
只需用反斜杠转义双引号。
回答by Gaurav
<?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
回答by Flask
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
回答by VeroLom
You can use PHP heredoc syntax:
您可以使用 PHP heredoc 语法:
var $js = <<<JS
var $js = <<<JS
// code
// 代码
JS;
JS;
Escapes and echo "<html_code>" is a noob style.
Escapes and echo "<html_code>" 是一种菜鸟风格。
回答by Reporter
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.
到目前为止。但要注意屏蔽引号和反斜杠等。
回答by Cloudson
Write html tags inside php code isn't nice. Read about templates to php as Smarty
在 php 代码中写 html 标签不好。阅读模板到 php as Smarty