JQUERY - 来自同一类元素的特定值

发布于 2024-11-09 21:59:50 字数 1527 浏览 0 评论 0原文

编辑:我昨晚真的很累,并且写错了我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦回旧景 2024-11-16 21:59:50

试试这个:

$hiddenheight=200;
$extraspace=100;

$(".stretch")
.each(function(){
    $(this).data('actualheight', $(this).outerHeight()+$extraspace).css('height', $hiddenheight+'px');    //outerHeight for including padding+margin+border, if any
})
.toggle(
    function(){
        $(this).animate({height: $(this).data('actualheight')+'px'}, 750);
        $(".readMore").text("Read Less");
    },
    function(){
        $(this).parent('div').animate({height:$hiddenheight+"px"}, 250);
        $(".readMore").text("Read More");
    }
)

根据需要调整高度值。

try this:

$hiddenheight=200;
$extraspace=100;

$(".stretch")
.each(function(){
    $(this).data('actualheight', $(this).outerHeight()+$extraspace).css('height', $hiddenheight+'px');    //outerHeight for including padding+margin+border, if any
})
.toggle(
    function(){
        $(this).animate({height: $(this).data('actualheight')+'px'}, 750);
        $(".readMore").text("Read Less");
    },
    function(){
        $(this).parent('div').animate({height:$hiddenheight+"px"}, 250);
        $(".readMore").text("Read More");
    }
)

adjust the height values according to needs..

说不完的你爱 2024-11-16 21:59:50

$.height() 将返回元素的实际高度。存储它,在调用hide时重新使用它。

就像这样:

$(".stretch").toggle(
    function(){
        $(this).data('oh', $(this).height());
        $(this).animate({height: 200}, 750);
    },
    function(){
        $(this).animate({height: $(this).data('oh')}, 250);
        $(this).data('oh', null);
    }
)

$.height() will return the actual height of your element. Store it, re-use it when calling hide.

Like so:

$(".stretch").toggle(
    function(){
        $(this).data('oh', $(this).height());
        $(this).animate({height: 200}, 750);
    },
    function(){
        $(this).animate({height: $(this).data('oh')}, 250);
        $(this).data('oh', null);
    }
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文