【问题标题】:PHP- insert innerhtml with javascript dynamically in each row of tablePHP-在表格的每一行中动态插入带有javascript的innerhtml
【发布时间】:2016-08-18 07:00:15
【问题描述】:

InnerHtml 在这种情况下不起作用,我想做这样的事情:

  <table width="100%" border="0" id="table2">
    
    <?php
     include("connection.php");
  $sql=mysql_query("select* from b1");
  while($res=mysql_fetch_array($sql))
  {
	$img=$res["img"];
	$url=$res["url"];
	  ?>
    <tr>
      <td><script>document.body.innerHTML="<a href='"+<?php echo '$url' ; ?>.value+"'><img src='"+<?php echo '$img' ; ?>+"' / ></a>" ;</script></td>
    </tr>
    <?php
  }
  ?>
  </table>

【问题讨论】:

    标签: javascript php html dom innerhtml


    【解决方案1】:

    你为什么不这样做呢?

      <table width="100%" border="0" id="table2">
        
        <?php
         include("connection.php");
      $sql=mysql_query("select* from b1");
      while($res=mysql_fetch_array($sql))
      {
    	$img=$res["img"];
    	$url=$res["url"];
    	  ?>
        <tr>
          <td><a href="<?php echo $url ; ?>"><img src="<?php echo $img; ?>" / ></a></td>
        </tr>
        <?php
      }
      ?>
      </table>

    【讨论】:

    • 我同意这个解决方案 - 但不要忘记在图像中添加 ALT 文本和宽度/高度属性,mysql_query 也是旧的,现在已弃用 - 尝试 PDO 或 mysqli
    猜你喜欢
    • 2019-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-06
    • 2021-01-17
    • 1970-01-01
    相关资源
    最近更新 更多