javascript - 从 HTML Table 中获取文本并将其放置在预定义的字符串中
问题描述
如何获取第一个 TD 的值并将其放在 jquery 中的预定义字符串中?
我的尝试:
$('td:first-child').each(function() {
console.log($(this).text());
});
我现在如何将值作为参数放入以下字符串中,并可能删除空格并在其间添加点?
细绳:{{email_open::john.sample@mymail.com}}John Sample{{email_close}}
我以为我需要 Jquery 中的 wrap 函数还是我错了?
<table>
<thead>
<tr>
<th>Name</th>
</tr>
</thead>
<tbody>
<tr>
<td>John Sample</td>
</tr>
</tbody>
</table>
解决方案
您可以使用类似toLowerCase()
andtrim()
来删除空格并降低文本:
var namemerge= $(this).text().toLowerCase().trim();
要替换单词之间的空格,您可以使用此答案中建议的正则表达式并附加邮件后缀:
namemerge= namemerge.replace(/ /g, '.')+'@mymail.com';
$('td:first-child').each(function() {
console.log($(this).text());
var namemerge= $(this).text().toLowerCase().trim();
console.log(namemerge);
namemerge= namemerge.replace(/ /g, '.')+'@mymail.com';
console.log(namemerge);
});
// I believe you can continue from this
console.log('{{email_open::john.sample@mymail.com}}John Sample{{email_close}}');
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
<thead>
<tr>
<th>Name</th>
</tr>
</thead>
<tbody>
<tr>
<td>John Sample</td>
</tr>
</table>
推荐阅读
- c# - 在 DDD 聚合中保持“当前余额”
- neo4j - Neo4j 在 cypher 单节点关系中整合节点
- python - 无法绘制网络图
- python - 将列表的值与多个列表进行比较
- wordpress - 将随机图像集成为电子邮件签名
- flutter - bloc 测试输出意外结果
- javascript - 从单独的 JavaScript 文件调用函数
- python - 无论输入如何,LSTM 层(keras)都会导致其之后的所有层不断预测相同的事物
- microsoft-graph-api - Microsoft Graph:有没有办法取消取消重复事件中的重复?
- android - 由于浏览器默认设置为桌面站点,因此深层链接不会在 Android 平板电脑中打开应用程序