我需要使用jQuery从页面元素中提取一些文本,但我无法弄清楚如何获取所需的内容。
这是我的HTML: Soulstar
var this_artist = $(this).children().text(); alert(this_artist);
这给了我“by Musiq Soulchild”(因为此处的this是a元素)。然后我假设只需要在.children()方法内指定一个选择器来缩小到.artist类,像这样:
var this_artist = $(this).children(".artist").text(); alert(this_artist);
这是我的HTML: Soulstar
by Musiq Soulchild
我想提取.artist的内容(在这种情况下是Musiq Soulchild)。首先,我尝试了以下代码:var this_artist = $(this).children().text(); alert(this_artist);
这给了我“by Musiq Soulchild”(因为此处的this是a元素)。然后我假设只需要在.children()方法内指定一个选择器来缩小到.artist类,像这样:
var this_artist = $(this).children(".artist").text(); alert(this_artist);
但这给我...什么都没有。我也尝试过$(this).children("span").text();
,同样地,我什么也没得到。
我做错了什么?
this
,我们需要知道代码执行的上下文。例如,它是否在事件处理程序内部? - Chris Laplante<a>
元素,因为它在附加到特定<div>
内所有锚点的函数中:$("#results a").live("click", function(event){...});
- daGUY.live(
已经在 1.7.1 版本中被废弃,建议使用.on(
。 - Mark Schultheiss