删除 >通过使用 javascript/jquery 识别正确的文本节点从列表中

发布于 12-19 01:23 字数 478 浏览 6 评论 0原文

今天下午我问了一个有点类似的问题,然后意识到答案是我的问题不正确......

考虑到以下情况:

<p class="p"> 
<div> </div> 
<div>
<a href="foo"> a </a>  &gt;
<a href="foo1"> b </a> 
<a href="foo2"> c</a> 
<a href="foo3"> b </a>  &gt;
<a href="foo4"> c </a> 
</div>
</p>

我需要从页面中删除“&gt”`以及a标签在它之前。这是一个动态列表,因此它会随着时间的推移而不断增长。我需要有人抓住“>”删除它,然后删除它前面的 a 标签。

有什么建议吗?

I asked a somewhat similar question this afternoon and then realized as great as the answer is my question was not correct...

given the following:

<p class="p"> 
<div> </div> 
<div>
<a href="foo"> a </a>  >
<a href="foo1"> b </a> 
<a href="foo2"> c</a> 
<a href="foo3"> b </a>  >
<a href="foo4"> c </a> 
</div>
</p>

I need to remove the ">"` from the page as well as the a tag that precedes it. This is a dynamic list so it will continously grow over time. I need to someone latch onto the ">" remove it and then remove the a tag before it.

Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

还在原地等你2024-12-26 01:23:17

您的标记无效。

元素将从

中踢出。

更正标记后,您可以执行以下操作:

var els = $('.p');

do {
    els.contents().each(function(i,el) {
        if( el.nodeType === 3 && $.trim( el.data ) === '>' ) {
            el.parentNode.removeChild(el.previousSibling);
            el.parentNode.removeChild(el);
        }
    });
} while( ( els = els.children() ).length )

JSFIDDLE DEMO


或者使用更多 jQuery:

var els = $('.p');

do {
    els.contents().each(function(i,el) {
        if( el.nodeType === 3 && $.trim( el.data ) === '>' ) {
            $(el.previousSibling).remove();
            $(el).remove();
        }
    });
} while( ( els = els.children() ).length )

JSFIDDLE DEMO


或者这个:

var els = $('.p');

do {
    els.contents().each(function(i,el) {
        if( el.nodeType === 3 && $.trim( el.data ) === '>' ) {
            $(el.previousSibling).add(el).remove();
        }
    });
} while( ( els = els.children() ).length )

JSFIDDLE 演示

Your markup is invalid. The <div> elements are going to be kicked out of the <p>.

After correcting your markup, you could do this:

var els = $('.p');

do {
    els.contents().each(function(i,el) {
        if( el.nodeType === 3 && $.trim( el.data ) === '>' ) {
            el.parentNode.removeChild(el.previousSibling);
            el.parentNode.removeChild(el);
        }
    });
} while( ( els = els.children() ).length )

JSFIDDLE DEMO


Or with a little more jQuery:

var els = $('.p');

do {
    els.contents().each(function(i,el) {
        if( el.nodeType === 3 && $.trim( el.data ) === '>' ) {
            $(el.previousSibling).remove();
            $(el).remove();
        }
    });
} while( ( els = els.children() ).length )

JSFIDDLE DEMO


Or this:

var els = $('.p');

do {
    els.contents().each(function(i,el) {
        if( el.nodeType === 3 && $.trim( el.data ) === '>' ) {
            $(el.previousSibling).add(el).remove();
        }
    });
} while( ( els = els.children() ).length )

JSFIDDLE DEMO

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文