封装:
(function ($) {
$.fn.maxHeight = function () {
插件代码...
};
})(jQuery);
调用:
//调用
var tallest = $('div').maxHeight();
html代码:
<div class="div1" style="width: 200px;height: 300px"></div>
<div class="div2" style="width: 200px;height: 400px"></div>
<div class="div3" style="width: 200px;height: 500px"></div>
<div class="div4" style="width: 200px;height: 600px"></div>
css代码:
div{
background: red;
margin-top: 20px;
}
js代码:
//jquery库封装方法
(function ($) {
$.fn.maxHeight = function () {
var max = 0;
console.log(this);
this.each(function () {
max = Math.max(max, $(this).height());
});
return max;
};
})(jQuery);
//调用
var tallest = $('div').maxHeight(); //返回高度最大的div元素的高度
console.log(tallest)