gpt4 book ai didi

jquery - 将 ID 附加到 div 内的最后一个 div

转载 作者:行者123 更新时间:2023-12-01 02:54:03 26 4
gpt4 key购买 nike

有人可以帮我关注吗?我是 jquery 新手,非常感谢您的帮助。如果窗口宽度小于 768 px,我需要将 ID 附加到 div.cenik 的最后一个子级?感谢您的帮助。

<div class="cenik">
<div>bla bla</div>
<div>bla bla</div>
<div>bla bla</div>
<div>bla bla</div> <-- this should get the ID-->
</div>

我的代码(有问题):

 (function($) {$(document).ready(function(){
if ($(window).width() < 768) {
$('.cenik').children().last(function(i) {
$(this).attr('id', 'cenik');
});
}
});})( jQuery );

最佳答案

这是固定代码:

(function ($) {
$(document).ready(function () {
if ($(window).width() < 768) {
$('.cenik').children().last().attr('id', 'cenik');
}
});
})(jQuery);

也就是说,我猜应该这样做:

(function ($) {
$(window).on('load resize', function () {
if (window.matchMedia("(max-width: 768px)").matches) {
$('.cenik').children().last().attr('id', 'cenik');
}
});
})(jQuery);

关于jquery - 将 ID 附加到 div 内的最后一个 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31543812/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com