首页 > 解决方案 > 从 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>

标签: javascriptjqueryhtmlhtml-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>


推荐阅读