开发者

More efficient way script the heights of DIVs

开发者 https://www.devze.com 2023-03-23 01:07 出处:网络
Im using this script to find the height of a DIV. I am using it on more than one DIV. Is there a more efficient way to write this code?

Im using this script to find the height of a DIV. I am using it on more than one DIV. Is there a more efficient way to write this code?

$(document).ready(function() {
    $(".block00"开发者_StackOverflow中文版).height($(".subheader").height());
    $(".block01").height($(".subheader").height()); 
    $(".block02").height($(".subheader").height());
});


No need to list each one separately or make a loop as you can just list multiple items in the selector and it will return all of them.

$(document).ready(function() {
    $(".block00, .block01, .block02").height($(".subheader").height());
});

or a little more efficiently:

$(document).ready(function() {
    var h = $(".subheader").height();
    $(".block00, .block01, .block02").height(h);
});

or, if you control the HTML source, add a common class on all the blockXX objects so you can do this:

$(document).ready(function() {
    var h = $(".subheader").height();
    $(".blockCommon").height(h);
});

Remember, you can have more than one class per object. Using a common class among several objects is precisely for the situation where you want to treat a number of objects the same way.


$(document).ready(function() {
  var h=$(".subheader").height();
  for(var i=0;i<3;i++)$(".block0"+i)height(h.height());
});

might work

0

精彩评论

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