动态构建对象 [英] Dynamically building objects

查看:51
本文介绍了动态构建对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好。我正在尝试在页面

加载后动态构建菜单和对象。数据存储在XML文件中,并在运行时解析为

Javascript对象。目前,我正在努力从这些对象创建菜单项

。解析工作正常(使用Sarissa),Firefox

构建菜单没问题。然而,IE却没有。

菜单的功能将是单个onclick事件。似乎Firefox允许我为动态构建的东西设置onclick事件处理程序,

但IE不会。


有没有人有关于此的任何信息,或者可能的替代

解决方案?谢谢。

解决方案

si *** **@gmail.com 写道:

大家好。我正在尝试在页面加载后动态构建菜单和对象。数据存储在XML文件中,并在运行时解析为Javascript对象。


这是一个坏主意。 XML作为向支持ECMAScript的客户端传输

数据的工具是次优的。 JSON要好得多,因为

ECMAScript解释器可以直接解析JS对象。

目前,我正在努力创建来自
的菜单项这些对象。解析工作正常(使用Sarissa),并且Firefox构建菜单没问题。然而,IE却没有。菜单的
功能将是单个onclick事件。它似乎Firefox允许我为动态构建的东西设置onclick事件处理程序,但IE不会。


IE提供了两种独立的机制,用于将事件处理程序附加到动态创建的DOM元素


有没有人有这方面的任何信息,或可能的替代解决方案?谢谢。




停止使用 - setAttribute - 方法来附加事件处理程序,就像

一样,它只是一个未指定的副作用。通过为

DOM元素的事件处理属性分配函数引用来附加事件处理程序

,或者使用分支W3C - addEventListener - / IE -

attachEvent - 进近。


Richard。


有趣。我没想过要使用addEventListener。但是,我现在已经尝试了它,并且正在使用点击按钮。这些菜单项的事件,

但它不起作用。它立即触发事件(函数

在页面加载时被调用),它不应该这样做,然后单击该项目的
什么都不做。是否有一些技巧使用

addEventListener? Haven还没有尝试过attachEvent用于IE,并且直到

我才能在Firefox中使用它。


至于XML和JSON,我担心这个特殊的

项目必须使用XML。这是与另一家软件公司签订的合同,他们希望

使用XML。


谢谢。


Richard Cornford写道:

这是一个坏主意。


这是一个荒谬的不合理的结论。

XML作为向启用ECMAScript的客户端传输数据的工具是次优的。


也许如果你只通过考虑数据的大小来定义次优的

交付。


其他可能有助于选择最佳因素的因素。解决方案

包括:

1)易于与现有系统交互

2)易于与其他开发商/公司互动

3)使用公司认可的技术或组件

4)使用DTD验证数据结构的要求

大多数开发人员可能熟悉XML以及用于操作它的工具。

大多数开发人员可能从未听说过JSON(我的同事之间没有#

我最近建议的时候。


因此,使用JSON保存额外的几kb数据传输可能

当你的时候不值得考虑所有其他现实因素。

JSON要好得多,因为
ECMAScript解释器可以直接解析它到JS对象。




这可能不是所需的功能,在这种情况下你需要编写

代码来解析对象。


-

马特Kruse
http://www.JavascriptToolbox.com


Hi all. I''m trying to dynamically build menus and objects after my page
loads. Data is stored in an XML file and is parsed at runtime into
Javascript objects. At the moment, I''m working on creating menu items
from these objects. The parsing works fine (using Sarissa), and Firefox
builds the menu no problem. IE, however, does not. The functionality of
the menu will be a single onclick event. It seems that Firefox allows
me to set the onclick event handler for something built dynamically,
but IE won''t.

Does anyone have any information on this, or a possible alternate
solution? Thanks.

解决方案

si*****@gmail.com wrote:

Hi all. I''m trying to dynamically build menus and objects
after my page loads. Data is stored in an XML file and is
parsed at runtime into Javascript objects.
That is a bad idea. XML is sub-optimal as a vehicle for transmitting
data to an ECMAScript enabled client. JSON is much better as the
ECMAScript interpreter can parse that directly to JS objects.
At the moment, I''m working on creating menu items from
these objects. The parsing works fine (using Sarissa), and
Firefox builds the menu no problem. IE, however, does not. The
functionality of the menu will be a single onclick event. It
seems that Firefox allows me to set the onclick event handler
for something built dynamically, but IE won''t.
IE provides two independent mechanisms for attaching event handlers to
dynamically created DOM elements.
Does anyone have any information on this, or a possible alternate
solution? Thanks.



Stop using the - setAttribute - method to attach event handlers as when
that works it is just an unspecified side effect. Attach event handlers
by assigning function references to the event handling properties of the
DOM elements, or use a branching W3C - addEventListener -/ IE -
attachEvent - approach.

Richard.


Interesting. I hadn''t thought of using addEventListener. However, I
have now tried it, and am using the "click" event for these menu items,
but it does not work. It fires off the event immediately (the function
gets called as the page loads), which it shouldn''t do, and then
clicking on the item does nothing. Is there some trick to using
addEventListener? Haven''t tried attachEvent yet for IE, and won''t until
I get it working in Firefox first.

As for XML and JSON, I''m afraid I have to use XML for this particular
project. It''s a contract with another software company, and they want
to use XML.

Thanks.


Richard Cornford wrote:

That is a bad idea.
That is a ridiculously unjustified conclusion.
XML is sub-optimal as a vehicle for transmitting
data to an ECMAScript enabled client.
Perhaps if you define sub-optimal by only considering the size of the data
delivered.

Other factors which may contribute to picking the "optimal" solution
include:
1) Ease of interacting with an existing system
2) Ease of interacting with other developers/companies
3) Use of company-approved technologies or components
4) The requirement to validate the structure of the data using a DTD

Most developers are probably familiar with XML and the tools used to
manipulate it.
Most developers have probably never heard of JSON (non of my co-workers had
when I suggested it recently).

Therefore, saving an extra few kb of data transmission by using JSON might
not be worth it when you consider all the other real-world factors.
JSON is much better as the
ECMAScript interpreter can parse that directly to JS objects.



Which may not be the desired functionality, in which case you need to write
code to parse the objects.

--
Matt Kruse
http://www.JavascriptToolbox.com


这篇关于动态构建对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆