所以基本上使用 jQuery,我尝试循环遍历包含推文的 3 个段落。这 3 段都是 .tweet-text 类
由于某种原因,在下面的代码中, $test2 将被正确设置为第一条推文的文本,但在每个循环中,每当我尝试设置 str.js 时,js就会崩溃。这是为什么?
var $tweets = $(".sidebar-tweet .tweet-text");
var $test2 = $tweets.text();
alert($test2);
$tweets.each(function(i, $tweet){
var str = $tweet.text();
alert(str);
});
我一直在尝试使用 Firebug 进行调试,但没有成功。感谢您的帮助。
最佳答案
var $tweets = $(".sidebar-tweet .tweet-text");
$tweets.each(function(i, current){
var str = $(current).text();
alert(str);
});
问题是当前对象默认不是 jquery 对象,您必须将其包装 $(current) ... 才能使用 jquery 方法
关于javascript - jQuery,循环段落并获取文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6472394/