[英]how to add id for an element in jquery
我有多个具有相同类名的元素。 但我想给一个用户的ID由用户单击的元素。
$('body').click(function(event) {
$(document).on('click', '.l1', function(){
$('.l1').removeAttr('id');
$('.l1').attr('id','selected');
});
$(document).on('hover', '.l1', function(){
alert($(this).attr('id'));
});
});
小提琴:
使用$(this)
以便指向您单击的元素
$(document).on('click', '.l1', function(){
$('.l1').removeAttr('id');
$(this).attr('id','selected');
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.