我有一个div,里面有很多东西。我在div中也有一个链接,它有一个动态生成的id。没有办法说出id会是什么。
当我点击该链接时,如何将该链接的id转换为变量jQuery?在* .js文件中?
答案 0 :(得分:2)
试试这个,
使用代码名称
$('a').click(function(){
//alert($(this).attr('id');
alert(this.id); //Better then the statement above as it is javascript with jQuery wrapper
});
使用课程
$('.classOfAnchor').click(function(){
alert($(this).attr('id');
alert(this.id); //Better then the statement above as it is javascript with jQuery wrapper
});
答案 1 :(得分:2)
$("a").click(function() {
//alert( $(this).attr('id') );
alert(this.id); //Better then above
});
答案 2 :(得分:0)
除非某些元素赋予元素一个id,否则就不会有一个。
但是,为了便于解释,我们可以说该链接肯定有一个ID ..
如果你使用jQuery,你可以简单地选择它的容器div,然后遍历到链接:var myID = $('div#theContainingDiv').children('a').attr('id');
或者,你可以做其他人所说的事情 - 这也会有用。