在JavaScript中转义PHP代码
Escaping PHP code inside JavaScript onmouseover
我很难理解如何在php块内的JavaScript事件中管理单引号和双引号。
这是代码:
<?php
$mainImagePath = '';
$galleryImages = $this->getGalleryImages();
if (count($galleryImages) > 0) {
$gallery = '<div class="more-views">';
$gallery .= '<h2>' . $this->__('More Views') . '</h2>';
$gallery .= '<ul>';
foreach ($galleryImages as $_image) {
if ($_image->getFile() == $_product->getData('small_image')) {
$mainImagePath = $this->getGalleryUrl($_image);
}
$gallery .= '<li>'
. '<a href="' . $this->helper('catalog/image')->init($this->getProduct(), 'image', $_image->getFile()) . '" '
. 'rel="popupWin:''' . $this->getGalleryUrl($_image) . ''', useZoom: ''cloudZoom'', smallImage: ''' . $this->getCloudImage($this->getProduct(), $_image) . '''" class="cloud-zoom-gallery" title="' . $this->htmlEscape($_image->getLabel()) . '" onmouseover="$(''image'').src = "'.$this->helper('catalog/image')->init($this->getProduct(), 'thumbnail', $_image->getFile())->resize(256).'"; return false;">'
. '<img src="' . $this->helper('catalog/image')->init($this->getProduct(), 'thumbnail', $_image->getFile())->resize(56) . '" width="56" height="56" alt="' . $this->htmlEscape($_image->getLabel()) . '" />'
. '</a></li>';
}
$gallery .= '</ul></div>';
}
?>
问题是onmouseover
事件,它有一个.src
方法,该方法期望值在双引号内,但是在该字符串内放入双引号会破坏其余的。
我已经试过把需要的值放在一个变量中,并回显变量,但这也不起作用。
如何正确转义引号?
onmouseover="$(''image'').src = "'.$this->helper('catalog/image')->init($this->getProduct(), 'thumbnail', $_image->getFile())->resize(256).'"; return false;">
使用单引号
onmouseover="$(''image'').src = '''.$this->helper('catalog/image')->init($this->getProduct(), 'thumbnail', $_image->getFile())->resize(256).'''; return false;">
我建议先写出html/javascript,然后简单地将php变量回显到正确的位置。你不需要做任何转义,这使得代码更容易维护。
您的IDE也将能够正确应用语法高亮
<?php
$mainImagePath = '';
$galleryImages = $this->getGalleryImages();
?>
<?php if(count($galleryImages) > 0) { ?>
<div class="more-views">
<h2><?php echo $this->__('More Views'); ?></h2>
<ul>
<?php foreach ($galleryImages as $_image) { ?>
<?php
if ($_image->getFile() == $_product->getData('small_image')) {
$mainImagePath = $this->getGalleryUrl($_image);
}
?>
<li>
<a href="<?php echo $this->helper('catalog/image')->init($this->getProduct(), 'image', $_image->getFile()); ?>" rel="popupWin:'<?php echo $this->getGalleryUrl($_image); ?>', useZoom: 'cloudZoom', smallImage: '<?php echo $this->getCloudImage($this->getProduct(), $_image); ?>'" class="cloud-zoom-gallery" title="<?php echo $this->htmlEscape($_image->getLabel()); ?>" onmouseover="$('image').src="<?php echo $this->helper('catalog/image')->init($this->getProduct(), 'thumbnail', $_image->getFile())->resize(256); ?>"; return false;">
<img src="<?php echo $this->helper('catalog/image')->init($this->getProduct(), 'thumbnail', $_image->getFile())->resize(56) ;?>" width="56" height="56" alt="<?php echo $this->htmlEscape($_image->getLabel()); ?>">
</a>
</li>
<?php } ?>
</ul>
</div>
相关文章:
- php代码在textbox更改事件上显示null
- 无法运行php代码,有角度路由问题
- jquery中的PHP代码不起作用
- 获取PHP代码中日期选择器的值
- PHP代码古埃及乘法javascript
- PHP代码中实现的JavaScript |if语句不起作用
- 如何将javascript变量传递到Php代码中.
- 在按钮'上运行PHP代码;s onclick事件,或者使用Ajax
- 停止直接执行php代码而不点击按钮
- php代码或脚本接受自动完成列表中文本框中的值
- 无法使autocompletion与bootstrap和php代码点火器一起工作
- 添加 PHP 代码的 Javascript 下拉框
- 在 php 代码中实现倒计时 js 倒计时
- AJAX 中的 PHP 代码到 POST
- 用于修改文档元素的 PHP 代码
- 链接适用于所有浏览器,除了 safari(php 代码)
- PHP 代码中的 CSS 弄乱了 IE9 中的布局
- Javascript的“document.write()”中的PHP代码
- JavaScript 函数在 php 代码中的调用中不起作用
- JavaScript 只在 php 代码中执行一次