开发者

How can I use jQuery to assign every element in a class its current height to its data-height attribute?

开发者 https://www.devze.com 2023-03-14 08:30 出处:网络
So if I were dealing with just one el开发者_JS百科ement, I could use the following: $(\"#id\").attr( \"data-height\" , $(\"#id\").css(\"height\") );

So if I were dealing with just one el开发者_JS百科ement, I could use the following:

$("#id").attr( "data-height" , $("#id").css("height") );

But is there any way to do this with every element in a class?


$('.myElem').each(function() {

    $this = $(this);
    $this.attr('data-height', $this.height());

});

You could also just use the .data() method

$('.myElem').each(function() {

    $this = $(this);
    $this.data('height', $this.height());

});


$('.className').children().each(function(i, obj) {
  $(obj).attr('data-height', $(obj).css('height'));
});
0

精彩评论

暂无评论...
验证码 换一张
取 消