JQUERY - 来自同一类元素的特定值
编辑:我昨晚真的很累,并且写错了我的 jQuery,“.readMore”始终是其各自 div 动画的触发器。抱歉。
大家好,
我想从同一类的元素中提取特定信息。我有多个包含不同数量文本的 div,因此它们的高度也不同。但是,我已将与这些 div 关联的类的高度设置为 200px。在每个 div 的底部,我有一个绝对定位的锚点,它允许读者将 div 扩展(动画)到它的自动/自然高度。
我想做的是找出具有相同类的 div 的“自动”高度,存储它,然后在单击相应 div 的锚点时调用它。到目前为止,我只获得一个值或没有值,而我想要所有值。
我最近一直在使用 .each() ,但没有效果。真正的问题似乎是div的动画,否则,没有问题;只需使用 .css() 将高度设置为自动即可。如果您可以将高度设置为自动,这会容易得多。我希望我能正确地解释自己。无论如何,这是我的代码。
HTML
<div class="stretch">
text
<a class="readMore">Read More</a>
</div>
<div class="stretch">
text
<a class="readMore">Read More</a>
</div>
<div class="stretch">
text
<a class="readMore">Read More</a>
</div>
CSS
div.stretch {height:auto; width:100%; overflow:hidden; position:relative}
a.readMore {position:absolute; left:0; bottom:0;}
JQuery
$divheight = //stored height of divs
$allheight = $divheight + 100; //extra space to clear the anchor
$(".h200").height(200);
$(".stretch").addClass("h200"); //change height later, to be able to refer to automatic/natural height
$(".readmore").toggle(
function(){
$(this).parent('div').animate({height:$allheight}, 750);
$(".readMore").text("Read Less");
},
function(){
$(this).parent('div').animate({height:"200px"}, 250);
$(".readMore").text("Read More");
}
)
提前感谢您,我真的很感谢您看一下。
EDIT: I was really tired last night, and wrote my jQuery out wrong, ".readMore" is always the trigger for it's respective div's animation. I apologize.
Hello Everyone,
I would like to extract specific information from elements with the same class. I have multiple divs that contain varying amounts of text, thus, their heights vary as well. However, I have set the class associated with these divs to a height of 200px. At the bottom of each div, I have an absolutely positioned anchor, that allows the reader to expand(animate) the div to it's automatic/natural height.
What I'd like to do is to find out the 'auto' height of the divs with the same class, store it, and then call on it later when a respective div's anchor is clicked. So far, I keep only getting one value or no value, whereas I'd like all of them.
I've been using .each() recently, but to no avail. The real problem seems to be animating the divs, otherwise, it's no problem; just need to use .css() to set the height to auto. This would be a whole lot easier if you could animate your height to auto. I hope I'm explaining myself correctly. In any case, here's my code.
HTML
<div class="stretch">
text
<a class="readMore">Read More</a>
</div>
<div class="stretch">
text
<a class="readMore">Read More</a>
</div>
<div class="stretch">
text
<a class="readMore">Read More</a>
</div>
CSS
div.stretch {height:auto; width:100%; overflow:hidden; position:relative}
a.readMore {position:absolute; left:0; bottom:0;}
JQuery
$divheight = //stored height of divs
$allheight = $divheight + 100; //extra space to clear the anchor
$(".h200").height(200);
$(".stretch").addClass("h200"); //change height later, to be able to refer to automatic/natural height
$(".readmore").toggle(
function(){
$(this).parent('div').animate({height:$allheight}, 750);
$(".readMore").text("Read Less");
},
function(){
$(this).parent('div').animate({height:"200px"}, 250);
$(".readMore").text("Read More");
}
)
Thanks it advance, I really appreciate you even taking a look.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
试试这个:
根据需要调整高度值。
try this:
adjust the height values according to needs..
$.height() 将返回元素的实际高度。存储它,在调用
hide
时重新使用它。就像这样:
$.height() will return the actual height of your element. Store it, re-use it when calling
hide
.Like so: