简体   繁体   English

jQuery调整大小处理程序不会对每个元素进行操作

[英]jQuery resize handler does not operate on each element

Edited since original posting 自原始发布以来已编辑

I have written a resize handler that listens for a parent div being resized (as a result of an ancestor container div being dragged). 我编写了一个大小调整处理程序,以侦听父div的大小调整(作为祖先容器div拖动的结果)。

The handler relies on the iframe method described here, which I had employed in my own code: Resize on div element 处理程序依赖于这里描述的iframe方法,我在自己的代码中使用了该方法: 在div元素上调整大小

I need to know that my divs have been resized as a result of the parent div being resized, and then do something as a result. 我需要知道,由于父div的大小已调整,因此div的大小也随之调整。 Note that this is not a window/browser resize event. 请注意,这不是窗口/浏览器调整大小事件。

When only one of my divs is on the page, it works fine. 当页面上只有我的一个div时,它工作正常。 When multiples are used, only the last one is affected by my script. 使用倍数时,只有最后一个受我的脚本影响。 I cannot figure out why and am now asking for help in figuring this out. 我不知道为什么,现在正在寻求帮助。

This has all come about during my attempts to scope the code so that it does properly apply itself to each one of my affected divs in turn. 这一切都是在我尝试对代码进行作用域期间实现的,以便它可以正确地依次将自身正确应用于每个受影响的div。

Fiddle here: https://jsfiddle.net/GrumJim/xpvt214o/939643/ 在这里提琴: https : //jsfiddle.net/GrumJim/xpvt214o/939643/

The offending code begins with: 令人反感的代码开始于:

$('.rsg_testtiles_display_macro_container').each(function(index) {

There are two divs in my markup, but only the second one is processed (12 outputs to the console represent the 12 child divs, but the five child divs in the first div are ignored) and I don't know why. 我的标记中有两个div,但是仅处理第二个div(控制台的12个输出代表12个子div,但是第一个div中的五个子div被忽略了),我不知道为什么。

All help much appreciated. 所有帮助,不胜感激。

It seems like you shouldn't actually iterate over the $(this) instance, since is already referring to a single element. 似乎您实际上不应该遍历$(this)实例,因为已经在引用单个元素。

Try to remove this line: $(this).each(function() { (and the appropriate closing of the function & parenthesis...) 尝试删除以下行: $(this).each(function() { (以及函数和括号的适当关闭...)

Mark Scultheiss's comment to the original question had the answer, thanks for letting me work that one out. 马克·斯库尔蒂斯(Mark Scultheiss)对原始问题的评论给出了答案,谢谢您让我解决这个问题。

I was most definitely calling things in the wrong order, particularly where I was assigning this to the var. 我绝对会以错误的顺序调用事物,尤其是在我将其分配给var的地方。 I have solved this by properly calling the on, then the each, then assigning this to the var and working through each of my divs in turn. 我已经通过正确地调用on,然后分别调用on,然后将其分配给var并依次遍历每个div来解决了这个问题。

I have far to go on my JavaScript journey, but you put me on the right path there. 我的JavaScript之旅还有很长的路要走,但是您使我走上了正确的道路。 Thanks folks! 谢谢大家!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM