javascript - 使用 innerHTML 更改页面上的所有文本

标签 javascript php html innerhtml php-5.5

我想更改页面上所有值为"is"的文本。如何更改页面上所有"is"值?

我只是想如果文本的值 = "Yes",则必须将其替换为 <span class='ic ic-normal ic-icon-yes'></span>

这是我当前的代码:

<?php
    $_helper = $this->helper('catalog/output');
    $_product = $this->getProduct()
?>
<?php if($_additionalgroup = $this->getAdditionalData()): ?>
<section id="additional">
<div class="box-collateral box-additional">
    <h2><?php echo $this->__('Additional Information') ?></h2>

    <?php $i=0; foreach ($_additionalgroup as $_additional): $i++; ?>
        <h3><?php echo $this->__( $_additional['title'] )?></h3>
        <table class="data-table" id="product-attribute-specs-table-<?php echo $i?>">
            <col width="25%" />
            <col />
            <tbody>
            <?php foreach ($_additional['items'] as $_data): ?>
             <?php $_attribute = $_product->getResource()->getAttribute($_data['code']);
    if (!is_null($_product->getData($_attribute->getAttributeCode())) && ((string)$_attribute->getFrontend()->getValue($_product) != '')) { ?>
                <tr>
                    <th class="label"><?php echo $this->htmlEscape($this->__($_data['label'])) ?></th>
                    <td class="data"><?php echo $_helper->productAttribute($_product, $_data['value'], $_data['code']) ?></td>
                </tr>
            <?php } ?>
            <?php endforeach; ?>
            </tbody>
        </table>
        <script type="text/javascript">decorateTable('product-attribute-specs-table-<?php echo $i?>')</script>
    <?php endforeach; ?>

</div>
</section>
<?php endif;?>

td 类中的文本是动态加载的。 元素没有任何唯一 ID。

我怎样才能做到这一点?

最佳答案

如果我正确理解你的问题,你想用某种模式更改所有包含等于"is"的文本的元素。试试这个脚本来实现:

$('*').filter(function() {
    return $(this).text()=='Yes'
}).each(function() {
    this.textContent = "<span class='ic ic-normal ic-icon-yes'></span>"
}); 

或者在纯 Javascript 中:

var allElements = document.body.getElementsByTagName("*");
for(var i = 0; i < allElements.length; i++) {
    var text = allElements[i].innerHTML;
    if (text == 'Yes') {
        allElements[i].innerHTML = "<span class='ic ic-normal ic-icon-yes'></span>";
    }
}

关于javascript - 使用 innerHTML 更改页面上的所有文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30259886/

相关文章:

javascript - 如何将光标移动到 contenteditable 实体的末尾

javascript - 从控制台和通过内容脚本执行相同代码时的输出差异

php - ongr.io的ElasticSearchBundle是否可以进行多索引搜索?

html - 滚动条移动居中内容的纯CSS解决方案

javascript - 转换 :hover menu to onclick for use on mobile

javascript - 如何调用Jquery数据表中的函数

javascript - 防止 jquery 使用 bootstrap 3 运行小分辨率?

javascript - 如何更改多个具有相同名称的类的每个值

php://input 返回空

PHP可以将类嵌套在两个文件中吗?