无法实现<cost>标签内循环文本批量替换,请求技术协助
解决标签内全局替换"One"为"Two"的问题
你的核心问题在于当前jQuery代码仅能替换第一个匹配的"One",且未处理所有<cost>标签,导致替换不彻底。以下是两种可行的解决方案:
方案一:修正jQuery代码(前端处理)
将字符串替换改为正则全局匹配,并遍历所有<cost>元素,确保所有匹配项都被替换:
$(document).ready(function() { // 遍历页面中所有<cost>标签 $("cost").each(function() { // 使用正则全局替换所有"One"为"Two" $(this).html($(this).html().replace(/One/g, "Two")); }); });
关键说明:
- 正则表达式
/One/g中的g标记表示全局匹配,会替换当前<cost>标签内所有出现的"One" .each()方法确保页面中所有<cost>标签都被处理,而非仅第一个$(this)指向当前遍历的<cost>元素,避免批量操作导致的内容冲突
方案二:PHP端直接替换(更高效)
如果"One"是从数据库取出的内容,建议在服务器端完成替换,减少前端不必要的处理:
修改PHP循环输出的代码,对每个字段使用str_replace:
while($row1 = mysqli_fetch_array($result1)) { echo ' <tr> <td><font face="Verdana">'.str_replace("One", "Two", $row1["name"]).'</font></td> <td><font color=#1341EB face="Verdana"><b>'.str_replace("One", "Two", $row1["len"]).'</b><input type="checkbox" class="checkbox-offers" name="checkbox" value="'.$row1["len"].'" /></font></td> <td><font color=#1341EB face="Verdana"><b>'.str_replace("One", "Two", $row1["myt"]).'</b><input type="checkbox" class="checkbox-offers" name="checkbox" value="'.$row1["myt"].'" /></font></td> <td><font color=#1341EB face="Verdana"><b>'.str_replace("One", "Two", $row1["tr"]).'</b><input type="checkbox" class="checkbox-offers" name="checkbox" value="'.$row1["tr"].'" /></font></td> <td><font color=#1341EB face="Verdana"><b>'.str_replace("One", "Two", $row1["kr"]).'</b><input type="checkbox" class="checkbox-offers" name="checkbox" value="'.$row1["kr"].'" /></font></td> <td><font color=#1341EB face="Verdana"><b>'.str_replace("One", "Two", $row1["var"]).'</b><input type="checkbox" class="checkbox-offers" name="checkbox" value="'.$row1["var"].'" /></font></td> <td><font color=#1341EB face="Verdana"><b>'.str_replace("One", "Two", $row1["kor"]).'</b><input type="checkbox" class="checkbox-offers" name="checkbox" value="'.$row1["kor"].'" /></font></td> </tr> '; }
原代码参考
PHP数据库查询代码
<?php $connect = mysqli_connect("localhost", "111", "password", "222"); $query1 ="SELECT * FROM beta ORDER BY ID DESC"; $result1 = mysqli_query($connect, $query1); ?>
原jQuery替换代码
<script type="text/javascript"> $(document).ready(function() { $("cost").html($("cost").html().replace('One', 'Two')); }); </script>
原HTML结构代码
<cost> <div class="table-responsive"> <table id="employee_data" class="row-border order-column cell-border hover"> <thead> <tr> <th>Name</th> <th width="10%">1</th> <th width="10%">2</th> <th width="10%">3</th> <th width="10%">4</th> <th width="10%">5</th> <th width="10%">6</th> </tr> </thead> <?php while($row1 = mysqli_fetch_array($result1)) { echo ' <tr> <td><font face="Verdana">'.$row1["name"].'</font></td> <td><font color=#1341EB face="Verdana"><b>'.$row1["len"].'</b><input type="checkbox" class="checkbox-offers" name="checkbox" value="'.$row1["len"].'" /></font></td> <td><font color=#1341EB face="Verdana"><b>'.$row1["myt"].'</b><input type="checkbox" class="checkbox-offers" name="checkbox" value="'.$row1["myt"].'" /></font></td> <td><font color=#1341EB face="Verdana"><b>'.$row1["tr"].'</b><input type="checkbox" class="checkbox-offers" name="checkbox" value="'.$row1["tr"].'" /></font></td> <td><font color=#1341EB face="Verdana"><b>'.$row1["kr"].'</b><input type="checkbox" class="checkbox-offers" name="checkbox" value="'.$row1["kr"].'" /></font></td> <td><font color=#1341EB face="Verdana"><b>'.$row1["var"].'</b><input type="checkbox" class="checkbox-offers" name="checkbox" value="'.$row1["var"].'" /></font></td> <td><font color=#1341EB face="Verdana"><b>'.$row1["kor"].'</b><input type="checkbox" class="checkbox-offers" name="checkbox" value="'.$row1["kor"].'" /></font></td> </tr> '; } ?> </table> </div> </cost>
内容的提问来源于stack exchange,提问作者John Bolgov
相关产品推荐
相关产品推荐

