简体   繁体   English

将Window Resize事件适当地附加到每个元素

[英]Attaching Window Resize event to each element saparately

I've been trying to figure out how, can I attach a Windows Resize event separately to each element (these elements are appended dynamically by PHP via ajax invoke). 我一直在尝试找出如何将Windows Resize事件分别附加到每个元素(这些元素由PHP通过ajax调用动态附加)。

The problem I also have is that I am utilizing global variables ie the variable id . 我还有一个问题是我正在使用全局变量,即变量id

I am trying to figure out how I can attach Window Resize even separately to each element. 我试图弄清楚如何将Window Resize甚至分别附加到每个元素。

EDIT: Here's a screenshot. 编辑:这是屏幕截图。

在此处输入图片说明

If you are using jQuery, you can attach custom events to any element. 如果使用的是jQuery,则可以将自定义事件附加到任何元素。

So, add the event "resize" (this event only exist for window element, so here, it's a custom event, you can name it like you want) 因此,添加事件“调整大小”(此事件仅存在于window元素中,因此,这是一个自定义事件,您可以根据需要命名它)

$("#" + id).on("resize", function(){

    // Do stuff here

});

And trigger it from window resize 并从窗口调整大小触发它

$(window).on("resize", function(){

    $("#" + id).trigger("resize");

});

EDIT : 编辑:

If you want that every "#" + id has "resize" event, event if appended in document later. 如果希望每个"#" + id都有“调整大小”事件,则该事件稍后会附加在文档中。

$(document).on("resize", "#" + id, function(){

    // Do stuff here

});

It had to do with the PHP and the id it assigned to the element, so all three elements have the same ID. 它与PHP及其分配给该元素的id有关,因此所有三个元素都具有相同的ID。 That makes sense. 那讲得通。 So I modified the ID inline and realized the .each() method works. 因此,我内联修改了ID,并意识到.each()方法有效。

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

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