javascript - JQuery:在抓取 .text() 时用分隔符替换 <br/>

标签 javascript jquery html

我正在尝试从这样形成的 HTML 元素中获取一些文本:

<td>
  <br>
  48 main Ave
  <br>
  Virginia, VA 23000
  <br>
  (800) 789-7898
</td>

问题是保持格式,或者行之间的某种分离(它可以是任何类型的定界符,但可以显示中断)。我不能简单地使用 .text():

$('td').text()
// 48 main AveVirginia, VA 23000(800) 789-7898

我也不能使用 .html(),因为 .html 中实际上还有一堆其他垃圾。

是否有任何其他创造性的方法可以保留换行符或在
标记所在的位置放置某种分隔符?

谢谢!

编辑:首先,你们真棒!感谢大家花时间回复。

我认为使用替换是可行的方法。但是我发现一旦我这样做了,我就无法再抓取文本了。这是我的完整代码:

detailsArray = [];
$("tr[id*=Row]").each(function(index){

    type = $(this).attr("type");
    id = $(this).attr("idtag");

    if (id){
        $("div[detail*="+id+"] tr td").each(function(index){
            var content = $(this).html();
            content = content.replace(/<br>/g,'|'); 
            $(this).replaceWith(content);
            detailsArray 
                .push(type+', '+ $(this)
                  .text()
                );
        })
     }
});

问题是,$(this).text() 没有捕捉到变化。所以我试着把它分成两个不同的部分,像这样:

detailsArray = [];
$("tr[id*=Row]").each(function(index){

    type = $(this).attr("type");
    id = $(this).attr("idtag");

    if (id){
         //First we change it around
         $("div[detail*="+id+"] tr td").each(function(index){
            var content = $(this).html();
            content = content.replace(/<br>/g,'|'); 
            $(this).replaceWith(content);
         }
        //Then we grab it
        $("div[detail*="+id+"] tr td").each(function(index){
            detailsArray 
                .push(type+', '+ $(this)
                  .text()
                );
        })
     }
});

我看到页面上发生了变化,但现在抓取部分是空的。 detailsArray = [].

那么我该如何使用呢?谢谢!

编辑** 认为更好地查看 html 会有所帮助:

<div detail="27fdgd68_4_B_">
  <table border="0" cellpadding="10" cellspacing="0" width="100%" class="size2">    
    <tbody>
      <tr bgcolor="#FFF5CC">
        <td>
          <b>
            <a target="_blank" href="" onclick="script()">Big Properties</a>
          </b>
          <a target="_blank" href="http://www.bigproperties.com"></a>
          <br>
          48 main Ave
          <br>
          Virginia, VA 23000
          <br>
          (800) 789-7898
        </td>
      </tr>
    </tbody>
  </table>
</div>

同样,我们的目标是获取以下文本(假设使用“|”分隔符):

48 main Ave|弗吉尼亚州 23000|(800) 789-7898

最佳答案

使用replace()

使用示例:

var content = $('myElement').html();
content = content.replace(/<br>/g,','); 

要更新 html 代码,请使用 jQuery 函数 replaceWith()

$(".yourDivClass").replaceWith(content);

var content = $("div").html();
content = content.replace(/<br>/g,','); 

$("div").replaceWith(content);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
    <br/>
  4884 Travertine Ave
    <br/>
  Virginia Beach, VA 23462
    <br/>
  (757) 605-5234
</div>

关于javascript - JQuery:在抓取 .text() 时用分隔符替换 <br/>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30493714/

相关文章:

Javascript 数组对象未定义

javascript - 使用 Chrome 查找与 HTML 元素关联的所有 js 事件

javascript - JQuery动态改变html内容

javascript - 在 Vue 组件中动态换行文本

javascript - 选择输入数组中的命名元素

javascript - fullpage.js - 大于视口(viewport)的部分的正常滚动

jquery - 从通过 html() 加载的 <img> 中检索 width() 和 height() 信息

javascript - HTML5 音频在 Firefox 中从错误的位置开始

javascript - 在 JavaScript 中为 jsoup 设置 url

javascript - 使用 Javascript 的带有命名空间的 xPath XML 文件