jQuery获取文本
我有一个函数返回一些这样的HTML:
return ("some text");
我在$ .ajax中的成功回调函数中使用它:
$.ajax({ url: uri, cache: false, success: function(html){ $(tag).append(format(html)); } });
html
提供元素。 我想只检索没有标签的文本。 我尝试使用format(html).text()但它不起作用。 有任何想法吗?
如果要对HTML字符串执行jQuery操作,请先使用jQuery进行转换:
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 。
$('some text').text()
应该完成工作或在你的情况下:
$(tag).append($(format(html)).text());
$(tag).append($(format(html)).text());
这将采用文本“ ...
”,将其转换为jQuery对象,并提取文本,留下标记。