javascript - 根据值将 td 替换为图像

标签 javascript php html smarty

我正在使用 Smarty 开发一个项目。 我想在网页上显示数组的结果,并用图像替换“1”结果,用另一个图像替换“0”结果。 到目前为止,我可以获得我的结果,就像我想要的那样,我只需要进行此替换,我真的不知道这是否可能,如果可能,我将如何做到这一点。 这是我的 php 代码的一部分:

for ($c=0; $c<count($aLPO); $c++){
$aPG['P_I'] = $aLPO[$c]['P_I'];
$aPG['P_L'] = $aLPO[$c]['P_L']; 
$sGA = $aLPO[$c]['P_G_A'];
for ($i=0; $i<count($aGId); $i++){
        if (strpos($sGA , $aGId[$i]) !== false){
            $sGId = $aGId[$i]; 
            $aPG[$aG[$sGId ]] = '1';
        }
        else{
            $sGId = $aGId[$i];
            $aPG[$aG[$sGId ]] = '0';
        }   
}
array_push($aPO, $aPG);
unset($aPG);

}

$smarty->assign("aLPO", $aPO);

以及我的 .tpl 代码的一部分 (html/smarty)

...
<div id="display_table_Produit_Gammes">
        <table cellpadding=0 cellspacing=0 class="tbl_datas" id="table_Produit" >
        {if $iNbProduitsOptions == 0}
            <thead>
                <tr><th></th></tr>
            </thead>
        {else}
            <thead>             
                <tr>
                    <th width='8%'>ID</th>
                    <th width='10%'>P_L</th>
                    {foreach from=$aLG item=g name=lst}
                        <th>{$g.G_L} ({$g.G_I})</th>
                    {/foreach}
                </tr>
            </thead>
            <tbody>
                    {foreach from=$aLPO item=p name=lst}
                        <tr>
                        {foreach from=$p item=pval}                        
                                <td>{$pval}</td>                            
                        {/foreach}
                        </tr>
                    {/foreach}
            </tbody>    
            {/if}
        </table>
</div>

我需要更改的结果在我的标签中,因为它们是动态充电的,我对处理方式有点困惑。

预先感谢您的帮助,如果您需要,请随时向我询问更多信息(因为我不会英语,所以很难理解该主题的目的,我希望它足够清楚)

最佳答案

这是更好解释的答案: 在 smarty 中你可以使用:

{if $pval eq '0'}
    <img src="image0.jpg">
{else}
    <img src="image0.jpg">
{/if}

如果使用 javascript 来执行此操作,您可以编写:

{foreach from=$p item=pval}                        
    <td class="replace_this_{$pval}"></td>                            
{/foreach}

为了向 td 添加一个类,然后在 javascript 中添加:

// select elements:
var elems = document.getElementsByClassName("replace_this_0");
// replace the <td></td> with <td><img src="image0.jpg"></td>
for(var i= 0; i< elems.length; ++i)
{
  elems[i].innerHTML = "<img src='image0.jpg'>";
}

第二张图片也是如此。希望这会有所帮助。

关于javascript - 根据值将 td 替换为图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29801884/

相关文章:

PHP 日期时间 DST

javascript - 如何为隐藏的输入和自定义标签提供相同的行为

html - Safari 6 - 影响整个表单的自动完成

javascript - SEC7118 : XMLHttpRequest CORS - IE Console message

php - 谷歌分析 API : filter by multiple URIs?

php - 使用 PHP 将 JSON 转换为来自 SQL DB 的字符串

javascript - this.class 与 this.getAttribute ('class' )

javascript - 如何将每个数组元素插入另一个数组中的每个其他元素?

javascript - 达到字数时启用提交按钮

javascript - 在 clearInterval() 之后恢复定时器