jQuery - 遍历所有动态td元素

时间:2015-07-22 06:54:10

标签: javascript php jquery

我正在使用PHP函数来创建表,我想使用弹出窗口为它生成的每个表显示一个图像。为了做到这一点,我必须循环遍历所有表,这是我的jQuery代码:

<script>
$("td").each(function() {
    var imagelink = "img/asdasd";
    var image = '<img id="sImg" style="width: 300px; height: 100px;" src="<?php echo $sImage; ?>" onError="this.onerror=null;this.src=`/img/noimage.png`;">';
    $('[data-toggle="popover"]').popover({placement: 'bottom', content: image, html: true}); 
});
</script>

我使用了每个功能但它似乎不起作用。它只在1张桌子上显示1张图片。

谢谢!

这是用于生成表格的HTML代码:

<div class="container-fluid jumbotron">
<div class="assemblies-content">

<?php



    $result = $db->prepare("SELECT * FROM `scripts` WHERE `cName` = '$camp' ORDER BY ID DESC");
    $result->execute();
    if( !$result->rowCount( ) ) echo "<center><h2><br>There aren't any scripts for this champion!</h2>";
    else
    { ?>

    <table class="table">
    <thead>
      <tr>
        <th>Script name</th>
        <th>Author</th>
        <th>Download Link</th>          
        <th>GitHub Link</th>
        <th>Thread Link</th>
        <th>Rating</th>
        <th>Status</th>
      </tr>
    </thead>
    <tbody>

<?php       
        for($i=0; $row = $result->fetch(); $i++)
        {
                if( $row['Status'] == 'Working' ) $status = 'success';
                if( $row['Status'] == 'Working' ) $statustext = 'Working';
                if( $row['Status'] == 'Broken' ) $status = 'danger';
                if( $row['Status'] == 'Broken' ) $statustext = 'Broken';
                if( $row['Status'] == 'Unknown' ) $statustext = 'Unknown';
                if( $row['Status'] == 'Unknown' ) $status = 'info';
                $likeuri = $row['Likes'];
                $dislikeuri = $row['DisLikes'];
                if( $row['sImage'] == null) $sImage = 'img/noimage.png';
                else $sImage = $row['sImage'];

?>
<script>
$("td").each(function() {
    var imagelink = "img/asdasd";
    var image = '<img id="sImg" style="width: 300px; height: 100px;" src="<?php echo $sImage; ?>" onError="this.onerror=null;this.src=`/img/noimage.png`;">';
    $('[data-toggle="popover"]').popover({placement: 'bottom', content: image, html: true}); 
});
</script>

<?php       
            echo "<tr class = '$status' data-container='body' data-toggle='popover' data-trigger='hover' data-placement='bottom' data-content=''><td style='width: 250px'>".$row['sName']."</td><td style='width: 200px'>".$row['sAuthor']."</td><td><a href= '".$row['dLink']."' >Click</a></td><td><a href= '".$row['gLink']."' >Click</a></td><td><a href= '".$row['Thread']."' >Click</a></td><td style='width: 100px'><span style='float:left; padding-right:10px; padding-top:2px;'>".$row['Rating']."</span><form action='' method='post'><button style='padding: 0px 2px;' type='submit' name='Like' value='".$row['ID']."'><img src='img/like.png' alt='Like' align='middle'></button>
            <button style='padding: 0px 2px;' type='submit' name='DisLike' value='".$row['ID']."'><img src='img/dislike.png' alt='Dislike' align='middle'></button></form></td><td>$statustext</td>";
        }

?>
    </tbody>
  </table>
    <?php }  ?>
</div>

0 个答案:

没有答案