jquery - JQuery - 查找和替换文本
问题描述
我正在尝试仅将“产品编号/描述”的文本替换为“产品编号”。这是代码行:
<td><input type="checkbox" name="dessearch"> Product Number/Description</td>
我尝试了以下操作,但它也删除了输入:
$("#ordersearch-page #content table tr td").text(function () {
return $(this).text().replace("Product Number/Description", "test");
});
任何帮助将不胜感激!
解决方案
一种选择是替换 HTML 而不是文本。这将保留<input>
元素。
$("#ordersearch-page #content table tr td").html(function(i, oldhtml) {
return oldhtml.replace("Product Number/Description", "test");
});
但是,这也会丢失对<input>
元素的任何动态更改,例如用户输入的值或事件侦听器。
另一种选择是将文本放入嵌套元素中,以便您可以专门针对它。
$("#ordersearch-page #content table tr td span").text(function(i, oldtext) {
return oldtext.replace("Product Number/Description", "test");
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="ordersearch-page">
<div id="content">
<table>
<tr>
<td><input type="checkbox " name="dessearch "><span> Product Number/Description</span></td>
</tr>
</table>
</div>
</div>
推荐阅读
- java - 如何在 Spring Boot 中使用 @Valid 验证对象?
- javascript - 使用父/子路由文件的奇怪行为
- android - 在 gradle 构建工具之外运行 Android 单元测试 - 使用 java junit 命令行?
- sql - REGEXP_SUBSTR - 如何“避免”字符串中的括号
- reactjs - 使用 React 17 版本时无法要求图像
- android - 如何在 Android 中设置像 spotify 这样的 Instagram 故事?(我想与应用程序 url 分享图像作为故事)
- javascript - 我可以使用 sparkpost 发送 .ics 文件吗?(node.js)
- ios - 在 iOS Swift 上进行公钥固定时的 ASN1 标头
- amazon-quicksight - 让读者能够将过滤器集保存为书签
- mockito - 如何通过 Mockito.mockStatic(SpringUtils.class) 模拟 SpringUtils.getBean("beanName")