改变 jquery 的高度只能工作一次
我有一组产品列表,每个产品列表下面都有一个链接,上面有一个单击事件,单击该链接时,它会转到父类,找到下面的类“surf_prod1”并滑动切换它。 其中一个函数检查所有类别为“surf_prod”的div并获取最大的innerHeight,该值被传回并用作所有类别为“surf_prod”的div的高度。
这对于第一次点击有效,但是后续的点击没有效果,并且只有初始高度似乎有效果,不确定这是否是由于某种形式的缓存造成的?
希望有人可以提供帮助。
这是我的 html:
<div class="surf_prod">
<img height="300" border="0" alt="7'2" - Egg" src="product_images/takayama_egg_7_2_egg_l0242_front.jpg"/><br/>
<span style="height: 35px;"><b id="review1615">7'2" - Egg</b></span><br/>
<div valign="top" class="surf_prod1">
<p style="margin-bottom: 2px;"><b>N : </b>16 1/4</p>
<p style="margin-bottom: 2px;"><b>M : </b>21 1/4</p>
<p style="margin-bottom: 2px;"><b>T : </b>14 3/4</p>
<p style="margin-bottom: 2px;"><b>Th : </b>2 3/8</p>
</div>
<span style="margin: 0px;"><b>FINS</b></span><br/>
<div class="surf_prod1">
<p><b>Centre : </b>US 8" TAK 4.5</p>
<p><b>Side : </b>FCS M5</p>
</div>
<div class="review_box">
<a class="review_link" href="#review1615"><b style="cursor: pointer; text-decoration: underline; color: rgb(204, 0, 0);" title="Click For Review">Board Review</b></a>
<div style="text-align: left; display: none;" class="surf_prod1 review_body">If you're not quite ready to "rip and shred" on a short board, nor are you apt to "cruise" on a long board, then the Hawaiian Pro Designs Egg is the perfect solution. This model is an "eggy" looking shape that allows the rider to trim high on the wave in the middle of the board or race down the line on the tail, whichever the rider prefers. This board is also a perfect beginners board because of the overall forgiving shape that allows for a variety of mistakes without loss of speed or manoeuvrability.<br/><a href="http://www.surfcom.co.uk/app/content_prod/5/1615"><b>More</b></a></div>
</div>
</div>
和我的 JavaScript:
function equalHeight(group) {
var tallest = 0;
group.each(function() {
thisHeight = $(this).innerHeight(true);
if(thisHeight > tallest) {
tallest = thisHeight;
}
});
return tallest;
}
$(document).ready(function(){
$(".review_body").slideUp("fast");
$(".review_link").click(function(ev){
ev.preventDefault();
var link = $(this).attr('href');
//Get the review container to be able to manipulate the children of it
$(this).parent(".review_box").find(".surf_prod1").slideToggle("fast", function(){
var surf_prod_height = equalHeight($("div.surf_prod"));
$("div.surf_prod").height(surf_prod_height);
document.location = link;
});
});
});
示例 此处。
I've got a set of product listings that each have a link below them with a click event on it, when this is clicked it goes to the parent class, finds the class "surf_prod1" below and slideToggles it.
Where a function that checks through all divs with a class of "surf_prod" and gets the biggest innerHeight which is passed back and used as the height for all divs with class of "surf_prod".
This works fins for the first click, however subsequent ones, have no effect, and only the initial height seems to have effect, not sure if this is due to some form of caching?
Hopefully someone can help.
Here's my html:
<div class="surf_prod">
<img height="300" border="0" alt="7'2" - Egg" src="product_images/takayama_egg_7_2_egg_l0242_front.jpg"/><br/>
<span style="height: 35px;"><b id="review1615">7'2" - Egg</b></span><br/>
<div valign="top" class="surf_prod1">
<p style="margin-bottom: 2px;"><b>N : </b>16 1/4</p>
<p style="margin-bottom: 2px;"><b>M : </b>21 1/4</p>
<p style="margin-bottom: 2px;"><b>T : </b>14 3/4</p>
<p style="margin-bottom: 2px;"><b>Th : </b>2 3/8</p>
</div>
<span style="margin: 0px;"><b>FINS</b></span><br/>
<div class="surf_prod1">
<p><b>Centre : </b>US 8" TAK 4.5</p>
<p><b>Side : </b>FCS M5</p>
</div>
<div class="review_box">
<a class="review_link" href="#review1615"><b style="cursor: pointer; text-decoration: underline; color: rgb(204, 0, 0);" title="Click For Review">Board Review</b></a>
<div style="text-align: left; display: none;" class="surf_prod1 review_body">If you're not quite ready to "rip and shred" on a short board, nor are you apt to "cruise" on a long board, then the Hawaiian Pro Designs Egg is the perfect solution. This model is an "eggy" looking shape that allows the rider to trim high on the wave in the middle of the board or race down the line on the tail, whichever the rider prefers. This board is also a perfect beginners board because of the overall forgiving shape that allows for a variety of mistakes without loss of speed or manoeuvrability.<br/><a href="http://www.surfcom.co.uk/app/content_prod/5/1615"><b>More</b></a></div>
</div>
</div>
and my JavaScript:
function equalHeight(group) {
var tallest = 0;
group.each(function() {
thisHeight = $(this).innerHeight(true);
if(thisHeight > tallest) {
tallest = thisHeight;
}
});
return tallest;
}
$(document).ready(function(){
$(".review_body").slideUp("fast");
$(".review_link").click(function(ev){
ev.preventDefault();
var link = $(this).attr('href');
//Get the review container to be able to manipulate the children of it
$(this).parent(".review_box").find(".surf_prod1").slideToggle("fast", function(){
var surf_prod_height = equalHeight($("div.surf_prod"));
$("div.surf_prod").height(surf_prod_height);
document.location = link;
});
});
});
Example here.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一旦你设置了高度,它就不再依赖于内容,所以第二次,你将获得所有 div 的相同高度——你第一次设置的高度。
在找到所需的高度之前,尝试将高度设置为“自动”:
Once you set the height, it no longer depends on the content, so the second time through, you are getting the same height for all of the divs--the height you set the first time.
Try setting the height to "auto" before you find the desired height: