获取封装在jquery对象中的html元素的直接内容 [英] getting the direct content of an html element encapsulated in a jquery object

查看:214
本文介绍了获取封装在jquery对象中的html元素的直接内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在jquery对象中有一段html。当我说$(this).html()时,我得到:

I have an piece of html in a jquery object. When I say $(this).html() I get:

<span class="value">4</span><span class="type">teaspoons</span>butter

我想要从这个不在跨度的html片段中只获取一段文本。在这个例子中,它是黄油。

I want to get only the piece of text from this html segment that is not in the spans. in this example, it is butter.

我如何得到它?

谢谢!

推荐答案

这是一种简单但有欺骗性的方式,可以放弃所有孩子并获取文本属性

There's the easy but cheating way, drop all the children and get the text property

var tmp = $(".post-text").clone();
tmp.children().remove();
tmp.text();

编辑还有 Text Children插件可以做到这一点。

EDIT There is also a Text Children plugin that does this.

这篇关于获取封装在jquery对象中的html元素的直接内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆