Jquery 元素高度作为 attr

Jquery element height as attr

只是快速简单的问题。 jquery 中是否可以在没有每个函数的情况下将元素的属性设置为 属性?

就像:

$('.google-maps').attr('data-desktop-height', this.height());

您可以提供一个函数作为第二个参数,它在选择器匹配的每个元素的上下文中工作:

$('.google-maps').attr('data-desktop-height', function() {
    return $(this).height()
});