Javascript 覆盖问题类 xml
也许这是一个基本的东西,但最好现在学习然后以后再学习。
我在做什么: 我正在实现一个获取 xml 的画廊,然后使用一些 javascript 代码构建我。我尝试调用两次 gallery.init 的问题如下:
$(document).ready(function(){
galleryXML.init({
xmlPath : "data/gallery.xml",
imgPath : "images",
perView : 4,
id: "#gallery1"
});
galleryXML.init({
xmlPath : "data/gallery.xml",
imgPath : "images",
perView : 4,
id: "#gallery"
});
})
我希望在 #gallery1 中调用一个,在 #gallery 中调用另一个。有人能告诉我有什么问题吗?
其余代码在这里只需下载、解压并执行索引即可.html(仅在 Firefox 中工作,也不知道为什么)
提前致谢。
更新
这是目前的一个最小示例,不确定是否能很好地代表问题。
Probably this is a basic stuff, but better learn now then later.
What i'm doing :
I 'm implementing a gallery that is getting a xml, and then build me using some javascript code. the problem i tried to call twice gallery.init like :
$(document).ready(function(){
galleryXML.init({
xmlPath : "data/gallery.xml",
imgPath : "images",
perView : 4,
id: "#gallery1"
});
galleryXML.init({
xmlPath : "data/gallery.xml",
imgPath : "images",
perView : 4,
id: "#gallery"
});
})
I expected to have one in #gallery1 other in #gallery. Can someone tell me what the problem?
The rest of the code is here only need to download, unrar and execute the index.html (Working just in firefox, not sure why too)
Thanks in advance.
UPDATE
This is a minimal example for now, not sure if represent well the problem.
http://jsfiddle.net/PSYCKIC/rpNab/2/ -> UPDATED
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看起来它在你的 jsFiddle 示例中工作得很好。您没有看到
li
显示的唯一原因是您忘记为该特定li
添加样式。这是更新的 jsFiddle ,您的代码保持相同和正确的 li 样式。Looks like it works just fine in your jsFiddle example. The only reason why you weren't seeing the
li
show up is because you forgot to add styling for that particularli
. Here's an updated jsFiddle with your code remaining the same and proper li styling.我认为您的问题可能是您正在为(您期望的)galleryXML 的 2 个不同实例使用相同的变量
_P
。_P
变量是在解析 javascript 代码时创建并初始化的,因为var galleryXML = function() {...}< 后面有
()
/代码>。所以我想如果您只需将变量放入 galleryXML 的 init 中,您的问题就会得到解决。您可以在此处查看代码:jsfiddle.net/rpNab/3/ (请注意,现在每个
li
都位于每个画廊内,而不是最后一个画廊中的两个li
)编辑: 我意识到现在用我的修改 galleryXML 模块看起来很难看(因为它只有一个方法并且没有变量),所以我做了一个小的重构,以便在该类中拥有更多的方法,但是现在的方法必须接收参数,因为类本身仍然是“静态”的,但是参数可以使其作用于不同的对象上下文。希望它有帮助: jsfiddle.net/rpNab/4/
I think your problem can be that you are using the same variable
_P
for (what you expect to be) 2 different instances of the galleryXML.The
_P
variable is created and initialized when the javascript code is parsed, because of the()
after thevar galleryXML = function() {...}
.So I guess your problem is going to be solved if you just put the variable inside the init of galleryXML. You can see the code here: jsfiddle.net/rpNab/3/ (notice that now each
li
is inside each gallery, instead of bothli
in the last gallery)EDIT: And I realize that now with my modification the galleryXML module seems ugly (because it only has one method and no variables), so I made a minor refactoring in order to have more methods inside that class, but the methods now must receive the parameter because the class itself continue to be "static", but the parameters can make it act for different contexts. Hope it helps: jsfiddle.net/rpNab/4/