问题,在HTML中返回脚本,需要第三'“
Issue, echoing HTML with script in it, in need of third ' "
<?php
echo '
<input type="file" name="image_path" style="visibility:hidden;" id="image_file">
<div class="input-append">
<input id="photoCover" class="input-large" type="text">
<a class="btn" onclick="$('input[id=image_file]').click();">Browse</a>
</div>
<script type="text/javascript">
$('input[id=image_file]').change(function() {
$('#photoCover').val($(this).val());
});
</script>
';
?>
如你所见,这在
处出现了冲突<a class="btn" onclick="$('input[id=image_file]').click();">Browse</a>
和
<script type="text/javascript">
$('input[id=image_file]').change(function() {
$('#photoCover').val($(this).val());
});
</script>
由于脚本开始'结束echo ',你会如何解决这个问题?对不起,如果这是一个愚蠢的问题
试试这个。这是可行的。
echo '
<input type="file" name="image_path" style="visibility:hidden;" id="image_file">
<div class="input-append">
<input id="photoCover" class="input-large" type="text">
<a class="btn" onclick="$(''input[id=image_file]'').click();">Browse</a>
</div>
<script type="text/javascript">
$("input[id=image_file]").change(function() {
$("#photoCover").val($(this).val());
});
</script>
';
?>
看起来您正在寻找heredoc语法。Heredoc语法类似于第三种类型的'
或"
。
echo <<<HEREDOC
<input type="file" name="image_path" style="visibility:hidden;" id="image_file">
<div class="input-append">
<input id="photoCover" class="input-large" type="text">
<a class="btn" onclick="$('input[id=image_file]').click();">Browse</a>
</div>
<script type="text/javascript">
$('input[id=image_file]').change(function() {
$('#photoCover').val($(this).val());
});
</script>
HEREDOC;
以<<<UNIQUEIDENTIFIER
开始您的heredoc语句,然后以无空格结束,UNIQUEIDENTIFER
,可能是;
,然后是换行符。关闭它的语法是非常特殊的,所以要小心使用。
见:http://www.php.net/manual/en/language.types.string.php language.types.string.syntax.heredoc
注意:您不必使用HEREDOC作为您的唯一标识符,但我倾向于这样做,因为它使那些不熟悉语法的人更容易有一些东西谷歌来弄清楚他们看到的是什么
简单的回答是使用反斜杠转义,如下所示:
<?php
echo '
<input type="file" name="image_path" style="visibility:hidden;" id="image_file">
<div class="input-append">
<input id="photoCover" class="input-large" type="text">
<a class="btn" onclick="$(''input[id=image_file]'').click();">Browse</a>
</div>
<script type="text/javascript">
$(''input[id=image_file]'').change(function() {
$(''#photoCover'').val($(this).val());
});
</script>
';
?>
您可以这样转义'
和"
:
$html = ' " '" '' ';
这样你可以得到3个不同的报价:
$html = '<script type="text/javascript">
$(''input[id=image_file]'').change(function() {
$(''#photoCover'').val($(this).val());
});
</script>';
在javascript代码中使用转义引号时也要小心,它位于PHP字符串中:
$html = '<script type="text/html">
var = ''This text has ''''quoted'''' single quotes :)'';
</script>';
我不知道你为什么要在PHP中重复这个,但你可以这样做:
<?php
// PHP Code
// In a PHP file, anything outside of PHP tags just gets echoed
?>
<input type="file" name="image_path" style="visibility:hidden;" id="image_file">
<div class="input-append">
<input id="photoCover" class="input-large" type="text">
<a class="btn" onclick="$('input[id=image_file]').click();">Browse</a>
</div>
<script type="text/javascript">
$('input[id=image_file]').change(function() {
$('#photoCover').val($(this).val());
});
</script>
<?php
// PHP Code
?>
您也可以使用Heredoc
语法:
<?php
$html = <<<HTML
<input type="file" name="image_path" style="visibility:hidden;" id="image_file">
<div class="input-append">
<input id="photoCover" class="input-large" type="text">
<a class="btn" onclick="$('input[id=image_file]').click();">Browse</a>
</div>
<script type="text/javascript">
$('input[id=image_file]').change(function() {
$('#photoCover').val($(this).val());
});
</script>
HTML;
echo $html;
?>
在这种情况下,你不需要担心引号和转义
由于这是PHP字符串结构中常见的事情,因此我将保留示例:
echo 'I want this text to show my ' in the sentence';
上面的例子是一个语法错误,因为字符串在预期之前关闭。但是,如果字符串中的单引号被转义,它将是有效的,并显示为:
echo 'I want this text to show my '' in the sentence';
//output : I want this text to show my ' in the sentence
其他方法是在双引号和单引号之间来回切换:
echo "This will show my ' in the sentence";
//output : This will show my ' in the sentence
但有时你需要更多,这样做看起来很难看:
echo "I want this ' in here but I need to quote ".'"poney"'." in my sentence";
//output : I want this ' in here but I need to quote "poney" in my sentence
所以要记得转义。使用HTML的另一种方法是退出PHP来显示它(有时不是一个选项):
<?php if(true) : ?>
<p>Show some html <?php echo $var;?> here</p>
<?php endif; ?>
或
<?php
//some code here
?>
<html>
</html>
这可以通过多种方式实现,并且可以避免在echo
- JS验证ajax返回的html中的表单数据
- php函数的ajax html$_POST值返回null
- Ajax调用返回当前html页面,而不是请求的文件
- 当使用ajax并将html数据保存为对象时,收听浏览器返回按钮.好的或坏的
- 统计ajax返回数据中的html元素
- HTML如何根据javascript函数的返回值限制文本输入
- 从JavaScript中正确返回html中的特殊字符
- 为什么我得到的是返回的[object HTML ParagraphElement]而不是我的号码
- 在ajax html返回中调用ajax会阻止第一个ajax脚本进一步工作
- jQuery filter 和 html 返回奇怪的结果
- 使用 Javascript 从 JSON 文件向 HTML 返回单个值
- JavaScript-使用selenium-webdriver将内部html返回给变量
- html返回中的Phantomjs编码问题
- 如何从HTML返回标签中获取值?
- 改变Html返回函数的元素
- 从用户处获取HTML输入,并将修改后的HTML返回给用户
- 如何获得html返回通过javascript跨域
- 用Jquery动态追加HTML返回[Object Object]
- 用OpenCPU显示R函数的html返回值
- 我如何执行内联javascript在HTML返回从ajax调用- JQUERY