jQuery获取文本 [英] jQuery get text

查看:93
本文介绍了jQuery获取文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个返回如下html的函数:

I have a function that returns some html like this:

return ("<span class='opt'>some text</span>");

我在$ .ajax的成功回调函数中使用它:

I use it within the success callback function in $.ajax:

$.ajax({
    url: uri,
    cache: false,
    success: function(html){
       $(tag).append(format(html));
    }
});

html给出<span></span>元素.我只想检索不带标签的文本.我使用format(html).text()尝试了它,但是它不起作用.有什么想法吗?

html give the <span></span> element. I want to retrieve just the text without the tags. I tried it using format(html).text() but it does not work. Any ideas?

推荐答案

如果要对HTML字符串执行jQuery操作,请先使用jQuery将其转换:

If you want to do jQuery operations on a string of HTML, use jQuery to convert it first:

var newContent = $(format(html)); // use the results of `format(html)` to make a jQuery selection
$(tag).append(newContent.text()); // use the jQuery text method to get the text value of newContent

请参阅API,以了解其工作原理.

这篇关于jQuery获取文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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