我想更改页面上所有值为"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/