gpt4 book ai didi

javascript - 使用 $(this).scrollWidth 获取实际宽度非常慢

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:36:22 27 4
gpt4 key购买 nike

我在做休闲

$('.Row .Cell .Text').each(function (index, item) {
if (this.scrollWidth > $(this).parent().width())
$(this).next().show();

else $(this).next().hide();
});

当我没有分配 $('.Row .Cell .Text') 时,一切都很好。但是如果我分配了行和单元格,上面的代码尤其是

this.scrollWidth

需要一定的时间。

知道如何才能更快地获得相同的东西吗?

添加了一个 fiddle jsFiddle

最佳答案

我猜您正试图隐藏 if .Text width > .Cell.Icon。参见下面的方法,

我尝试使用 filter 将 jQuery 代码移出侧循环。

CSS

/*set overflow for .Text - This lets you calculate the actual width of .Text*/
.Text { overflow: hidden; }

/* Hide .Icon by default and show only if .Text width < .Cell width*/
.Cell .Icon { display: none; }

JS

$('.Row .Cell .Text').filter(function (i, el) {    
return el.scrollWidth <= this.parentNode.scrollWidth; //Thanks @nicoabie
}).next().show();

演示: http://jsfiddle.net/nYSDy/4/

关于javascript - 使用 $(this).scrollWidth 获取实际宽度非常慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15134381/

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