我的代码如下:
<?php
$message = "<script type="text/javascript">
$(document).ready(function(){
demo.initChartist();
$.notify({
icon: 'pe-7s-gift',
message: "hello world."
},{
type: 'info',
timer: 4000
});
});
</script>";
?>
在html中
<?php echo $message; ?>
答案 0 :(得分:1)
最好的办法是使用HEREDOC,而不必担心报价。
你可以这样做:
$message = <<<JS
$(document).ready(function(){
demo.initChartist();
$.notify({
icon: 'pe-7s-gift',
message: 'hello world.'
},{
type: 'info',
timer: 4000
});
});
JS;
print '<script>'.$message.'</script>';
或者如果你需要将脚本标签放在变量中;你可以使用EOT而不是JS。
答案 1 :(得分:0)
只需将内部"
双引号替换为单引号'
<?php
$message = "<script type='text/javascript'>
$(document).ready(function(){
demo.initChartist();
$.notify({
icon: 'pe-7s-gift',
message: 'hello world.'
},{
type: 'info',
timer: 4000
});
});
</script>";
?>
答案 2 :(得分:0)
如果您尝试将echo
<script>
元素放到网页上,这应该有效。
唯一能阻挡你的是引号。现在,您尝试在由双引号组成的字符串中使用双引号。这只有在你转义字符串中的引号时才有效(这可以使用转义字符\
来完成,如"\""
)。
当然也可以用单引号替换双引号。
请查看this question以获取更广泛的示例/解释。