我想做以下事情:在标签里放一个标签。监听标签的click()事件。在click-event中,我想检查img标签是否有特定的属性。
例如:
<a href='#' id='myid'><img src='' alt='' played='1'></a>
$('#myid').click(function(e){
if(e.target.attr('played')){
// do something...
}
});
如何获取img标签的属性?我从不检查jquery中的元素处理程序...
致以亲切的问候!
发布于 2012-11-05 01:34:51
$('#myid').click(function(e){
if( $(this).find('img').attr('played') == 1)
{
// do something...
}
});
ps
最好使用'data-played‘作为属性名
发布于 2012-11-05 01:35:50
您可以这样做:
$('#myid').click(function(e){
e.preventDefault(); // stopping default behaviour of the anchor
var played_attribute = $(this).find(img).attr('played');
if(played_attribute == '1') {
//attribute had value of 1
} else {
//attribute either never existed or didnt have value of 1
}
});
希望能有所帮助。
发布于 2012-11-05 01:37:22
你应该做$(e.target).attr("played")
https://stackoverflow.com/questions/13221041
复制相似问题