我正在尝试检测一个元素是否有任何不特定的属性。我只想知道有没有如果有任何属性,我想删除它们并添加新属性。如果没有任何新属性,则只会添加。
我的代码是
if ($('.element').attr('style').length > 0) {
$(this).removeAttr('style');
}
$('.element').css({
"top": (posY),
"left":(posX)
});
但在控制台中显示 Uncaught TypeError: Cannot read property 'length' of undefined
谁能帮忙?
最佳答案
试试这个,
if($('.element').attr('style') && $('.element').attr('style').length > 0){
$('.element').removeAttr('style');
}
$('.element').css({
"top": (posY),
"left": (posX)
});
关于javascript - 有没有简单的方法来检测 attr jquery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30182178/