【问题标题】:Can't use <img src=""onclick='window.open('anotherpage.html','_blank");' /> attribute in HTML correctly不能使用 <img src=""onclick='window.open('anotherpage.html','_blank");' HTML 中的 /> 属性正确
【发布时间】:2014-04-25 08:55:11
【问题描述】:

今天我正在尝试使用StacOverflow的img属性代码,但没有成功。

<img src="..." alt="..." onclick="window.open('anotherpage.html', '_blank');" />

来自Here

image.php

<?php
$pic = array('1.jpg','2.jpg','3.jpg','4.jpg','5.jpg');
shuffle($pic);
?>

original.index.php 很好用

 <html>
 <head>
    <title>Random Images</title>
 </head>
 <body>
  <ul>
    <?php
include("img/image.php");
       for( $i = 0; $i < 3; $i++)
          echo "<li style=\"display: inline;\">
                         <img src=\"$pic[$i]\" width=\"250\" height=\"250\">
                       </li>";

    ?>  
 </ul>

 </body>
</html>

in.my.case.index.php

<html>
<head>
   <title>Random Images</title>
</head>
 <body>
  <ul>
    <?php
include("img/image.php");
       for( $i = 0; $i < 3; $i++)
          echo "<li style=\"display: inline;\">
                         <img src=\"$pic[$i]\" width=\"150\" height=\"130\" alt=\"Go to back\" onclick=\"window.open(\"anotherpage.php\", \"_blank\");\" />
                       </li>";

    ?>  
 </ul>

 </body>
</html>

不要严格判断

【问题讨论】:

    标签: php html image


    【解决方案1】:

    对函数参数以及 onclick 属性使用双引号会导致问题。

    修复示例:

    echo "<li style=\"display: inline;\">
            <img src=\"$pic\" width=\"150\" height=\"130\" alt=\"Go to back\" onclick='window.open(\"anotherpage.php\", \"_blank\");' />
        </li>";
    

    这是个人喜好,但您可能认为这种方法更具可读性:

    <?php for( $i = 0; $i < 3; $i++): ?>
    <li style="display: inline;">
        <img src="<?php echo $pic[$i]; ?>" width="150" height="130" alt="Go to back" onclick='window.open("anotherpage.php", "_blank");' />
    </li>
    <?php endfor; ?>
    

    【讨论】:

      【解决方案2】:

      尝试在javascript部分用单引号替换双引号:

      onclick=\"window.open('anotherpage.php', '_blank');\" />
      

      【讨论】:

        猜你喜欢
        • 2015-07-16
        • 2014-09-09
        • 2012-02-05
        • 2012-11-16
        • 1970-01-01
        • 2019-10-03
        • 1970-01-01
        • 1970-01-01
        • 2012-05-12
        相关资源
        最近更新 更多