本文介绍了遍历元素的Flex的元素中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在的Flex 4以下功能:
I have the following function in Flex 4:
protected function initEventHandlers():void
{
imageContainer.addEventListener(DragEvent.DRAG_ENTER, acceptDrag);
imageContainer.addEventListener(DragEvent.DRAG_DROP, handleDrop);
img_1.addEventListener(MouseEvent.MOUSE_DOWN, handleDrag);
img_2.addEventListener(MouseEvent.MOUSE_DOWN, handleDrag);
img_3.addEventListener(MouseEvent.MOUSE_DOWN, handleDrag);
img_4.addEventListener(MouseEvent.MOUSE_DOWN, handleDrag);
}
我不喜欢这个code的外观虽然。四象是我的应用程序中声明如下:
I didn't like the look of this code though. The four images are declared inside my application as follows:
<s:HGroup y="10" width="650" horizontalAlign="center" horizontalCenter="6">
<s:Image width="80" height="80" source="images/1.jpg" id="img_1" />
<s:Image width="80" height="80" source="images/2.jpg" id="img_2" />
<s:Image width="80" height="80" source="images/3.jpeg" id="img_3" />
<s:Image width="80" height="80" source="images/4.jpg" id="img_4" />
</s:HGroup>
还没有一个地遍历在hgroup每个图像,并添加事件处理程序?
Isn't there a way to loop over each image in the hgroup and add the eventhandler?
事情是这样的:
for(image in hgroup) {
image.addEventlistener(MouseEvent.MOUSE_DOWN, handleDrag);
}
我的老师告诉我,这是不可能的,但在情况下10+的图像,我无法想象的每张图片分开做。必须有一个更好的方式来做到这一点,不是吗?
My teacher told me this isn't possible but in case of 10+ images, I can't imagine doing it for every image separately. There has to be a better way to do this, no?
在此先感谢!
推荐答案
您的老师是不对的!
给出的HGroup一个ID(如imageGroup)。
Give the HGroup an id (e.g. imageGroup).
那么做到这一点:
var numElements:int = imageGroup.numElements;
for (var i:int = 0; i<numElements; i++) {
var image:Image= imageGroup.getElementAt(i) as Image;
if (image) image.addEventlistener(MouseEvent.MOUSE_DOWN, handleDrag);
}
这篇关于遍历元素的Flex的元素中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!