我使用 ajax 从 mysql 数据库加载一些数据...我的问题是,它获取我要加载的数据的 ID,我已将 HREF 值设置为 ID...所以一个例子是:
`<a href="16" title="View Story: ssasds">ssasds</a>`,
16 是我需要的 id 值...我的代码是:
$('.artefact').click(function()
{
var storyId = $('a',this).attr('href');
console.log(storyId);
}
当我检查控制台 (firebug) 时,它只是说未定义。请尝试并提供帮助,因为我尝试了其他获取数据的方法但变得困惑。
谢谢
最佳答案
似乎 .artefact
有两个 a
。基于此:
$('.artefact').click(function () {
var storyId = $('a', this).filter("[href]").attr('href');
console.log(storyId);
});
编辑
转念一想,这看起来更干净了:
$('.artefact').click(function () {
var storyId = $(this).find("a[href]").attr('href');
console.log(storyId);
});
关于jquery - $(a,this).attr ('href' ) 返回未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5620164/