jQuery或Javascript将所有按钮设置为最宽按钮的宽度
我的客户希望我编写一段代码,jQuery优先,将所有链接设置为最宽链接的相同宽度。以下是HTML:jQuery或Javascript将所有按钮设置为最宽按钮的宽度,javascript,jquery,html,css,Javascript,Jquery,Html,Css,我的客户希望我编写一段代码,jQuery优先,将所有链接设置为最宽链接的相同宽度。以下是HTML: <div> <h1>HEADER</h1> <p style="text-align: right;" class="floatRight"><a href="#" class="small-button eLength"><span>BUTTON 1</span></a></p> <
<div>
<h1>HEADER</h1>
<p style="text-align: right;" class="floatRight"><a href="#" class="small-button eLength"><span>BUTTON 1</span></a></p>
<p style="text-align: right;" class="floatRight"><a href="#" class="small-button eLength"><span>BUTTON 2</span></a></p>
<p style="text-align: right;" class="floatRight"><a href="#" class="small-button eLength"><span>BUTTON LONGEST</span></a></p>
</div>
标题
CSS将所有三个按钮的最小宽度设置为180px。我希望这一切都取决于课堂:eLength你可以像这样获得最大宽度:
var widest = Math.max.apply(Math, $('.eLength').map(function() {
return $(this).width();
}));
要将其封装在一个小插件中:
$.fn.widest = function() {
return this.length ? this.width(Math.max.apply(Math, this.map(function() {
return $(this).width();
}))) : this;
};
$('.eLength').widest();
使用John Resig的:
按钮及其标签是动态创建的吗?如果它们不是,我个人认为增加这种开销是一件愚蠢的事情。按钮是动态创建的警报出现最大宽度:-无穷大小心Max函数,传递除长度数组之外的任何内容都将返回
-无穷大。
Array.max = function(array) {
return Math.max.apply(Math, array);
};
var widths = new Array();
$('.eLength').each(function(index) {
widths.push($(this).width());
});
alert("Max Width: " + Array.max(widths));