I have a TD that always contains a div and some text outside the div.

我有一个TD总是包含div和div之外的一些文本。

The div has four nested divs that always follow the below structure. The only thing I am interested in is the text in this TD that appears outside the main div, everything else should be removed.

div有四个嵌套的div,它们总是遵循以下结构。我唯一感兴趣的是这个TD中出现在主div之外的文本,其他一切都应该删除。

The text I want to keep varies, the structure of the rest is fixed.

我想保留的文字各不相同,其余部分的结构是固定的。

Example TD:

示例TD:

<td>
    <div id="show_status_1622327228" class="nor_addr_status" onmouseout="show_status_div(2, this);" onmouseover="show_status_div(1, this);" style="display: none;">
        <div id="status_safe_1622327228" class="status_hov" onmouseout="javascript:chgcolor(2, this);" onmouseover="javascript:chgcolor(1, this);">
            Text1
        </div>
        <div id="status_unknown_1622327228" class="status_hov" onmouseout="javascript:chgcolor(2, this);" onmouseover="javascript:chgcolor(1, this);">
            Text2
        </div>
        <div id="status_pr_1622327228" class="status_hov" onmouseout="javascript:chgcolor(2, this);" onmouseover="javascript:chgcolor(1, this);" style="background-color: rgb(255, 255, 255);">
            Text3
        </div>
        <div id="status_fraud_1622327228" class="status_hov" onmouseout="javascript:chgcolor(2, this);" onmouseover="javascript:chgcolor(1, this);" style="background-color: rgb(255, 255, 255);">
            Text4
        </div>
    </div>
    The only text I want to keep
</td>

I tried the following which works fine. My only concern is the multiple replaces / Regexes and I am wondering if I could use .remove() instead in order to reduce loading times.

我尝试了以下工作正常。我唯一担心的是多次替换/ Regexes,我想知道我是否可以使用.remove()来减少加载时间。

If this is possible, can someone here tell me how I have to adjust it ? Note: The first line of the below needs to remain unchanged, this is only about the replace / Regex part below.

如果可以,有人可以告诉我如何调整它吗?注意:下面的第一行需要保持不变,这只是下面的替换/正则表达式部分。

util.setProp(obj, "address", $(msg).find("#tab_address_info")
    .find("td:contains('(Home)')").prev().text()
    .replace(/<div[\s\S]+?<\/div>/g, '')
    .replace(/\s+/g, ' '));

Many thanks for any help with this, Tim.

蒂姆,非常感谢你提供的任何帮助。

2 个解决方案

#1


1

As @Milind Anantwar said you can use .remove(). If you want to get the value of the text outside the divs to set somewhere else but not actually remove the divs from the page then you will need to .clone()before you .remove()

正如@Milind Anantwar所说,你可以使用.remove()。如果你想获得div之外的文本值来设置其他地方但实际上没有从页面中删除div,那么你需要在.clove()之前使用.clove()

I think maybe you want something like

我想也许你想要的东西

var td = $('td.cleantd').clone(); //Using comment above - otherwise find the right td yourself
$('div', td).remove();
util.setProp(obj, "address", td.text());

Working fiddle

工作小提琴

更多相关文章

  1. 使用jquery animate()实现对文本的反弹效果?
  2. Jquery禁用/启用按钮与文本框代码
  3. 获取复选框的文本属性
  4. jQuery中的bind绑定事件与文本框改变事件的临时解决方法
  5. 克隆文本并插入壁橱标题范围
  6. 如何删除/更改JQuery UI自动完成帮助文本?
  7. jQuery自动完成不适用于多个文本输入
  8. 如何使用jQuery UI插件使html文本框可拖动和调整大小?
  9. 在使用javascript将信息输入到输入字段时填充文本区域

随机推荐

  1. python的接口实现zope.interface示例
  2. dev-server HTTP错误403:禁止
  3. (四)OpenCV-Python学习—形态学处理
  4. [Python设计模式] 第1章 计算器——简单
  5. 在centos7中python3的安装注意
  6. Python3.6中PyInstaller不能对文件进行打
  7. 【实例】python re 正则表达式 同时选择
  8. 用积分来解拟合方程。
  9. The bytes/str dichotomy in Python 3
  10. python实现单例模式