【问题标题】:onClick event failingonClick 事件失败
【发布时间】:2014-08-04 04:47:10
【问题描述】:

我的功能是:

<head>
<script type="text/javascript">
// Info popup window
function showInfo(infFilename) {
alert(infFilename);
popupWin = window.open('notes/"+infFilename+"','open_window','width=400,height=300,toolbar=0,menubar=0,location=0,status=1,scrollbars=1,resizable=1,left=20,top=20' );
}
</script>
</head>

调用弹窗的php代码是:

<?php>
// check if metadata exists ( PicNotes is "filename.php" )
if ($row['PicNotes']) {
$icon = "<img src = \"images/info.png\"  onClick=\"showInfo($row[PicNotes])\" />";
}else{
$icon='';}

echo "<a href='images/".$row['vfile']."' rel='enlargeimage' rev='targetdiv:loadarea' title='&lt;b&gt;".$row['Title']."&lt;/b&gt;&lt;br /&gt;".$row['Medium']." &nbsp; &nbsp; &nbsp; ".$row['iw']." x ".$row['ih']." cm. $icon'><img border='1' src='images/".$row['tnfile']." ' alt=' '  width='".$row['tnx']."' height='".$row['tny']."' class='tn' /></a><br />";

?>

这一行的html是(来自查看源代码):

<a href='images/0712120189y.jpg' rel='enlargeimage' rev='targetdiv:loadarea' title='&lt;b&gt;Broken Bridge in Water Meadow&lt;/b&gt;&lt;br /&gt;Gouache &amp; Pastel &nbsp; &nbsp; &nbsp; 24 x 19 cm. <img src = "images/info.png"  onClick="showInfo(wm_series.php)" />'><img border='1' src='images/0712120189z.jpg ' alt=' '  width='96' height='75' class='tn' /></a>

请有人看看我做错了什么。

【问题讨论】:

  • showInfo('wm_series.php') 需要在名称周围加上引号。
  • 查看&lt;img 元素的HTML。您在 showInfo 的参数周围缺少引号。

标签: javascript xhtml


【解决方案1】:

这部分:

onClick=\"showInfo($row[PicNotes])\"

应该是:

onClick=\"showInfo('$row[PicNotes]')\"

文件名参数需要用引号引起来,因为它是一个字符串。

另外,你这里有一个字符:

cm. $icon'>

应该是:

cm. $icon

您的弹出代码应该是:

function showInfo(infFilename) {
    alert(infFilename);
    popupWin = window.open('notes/'+infFilename,'open_window','width=400,height=300,toolbar=0,menubar=0,location=0,status=1,scrollbars=1,resizable=1,left=20,top=20' );
}

【讨论】:

  • Thenks Barmar 但我已经尝试过了,但它会停止 php 正确处理该行。输出变为:
    &lt;a href='images/0712120189y.jpg' rel='enlargeimage' rev='targetdiv:loadarea' title='&amp;lt;b&amp;gt;Broken Bridge in Water Meadow&amp;lt;/b&amp;gt;&amp;lt;br /&amp;gt;Gouache &amp;amp; Pastel &amp;nbsp; &amp;nbsp; &amp;nbsp; 24 x 19 cm. &lt;img src = "images/info.png" onClick="showInfo('wm_series.php')" /&gt;'&gt;&lt;img border='1' src='images/0712120189z.jpg ' alt=' ' width='96' height='75' class='tn' /&gt;&lt;/a&gt;
  • 双引号并将它们转义会产生相同的结果。
  • 您不能使用双引号,因为您已经在 onclick 属性本身周围使用了双引号。 HTML 属性不支持转义,尽管我认为您可以在其中使用 &amp;quot;。但是单引号应该像我写的那样起作用。它们与属性周围使用的引号相反,因此没有冲突。
  • 渲染不正确,查看源显示全部为红色,结束标记呈现为文本。
  • 刚试过&quot: onclick 语句然后呈现为 ​​onClick="showInfo("wm_series.php")" 但弹出窗口仍然没有触发
猜你喜欢
  • 2013-09-15
  • 1970-01-01
  • 2016-09-11
  • 2017-03-18
  • 2013-12-25
  • 2014-04-22
  • 2013-12-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多