MooTools 使用抓取/注入来附加子元素
如何将一个新元素附加到另一个元素中?下面的代码使用了图书馆网站上的示例,但没有呈现任何内容。我使用渲染的 .innerHTML 测试了包含元素。我做错了什么?
var Article = new Class({
initialize: function(order, title, body){
this.order = order;
this.title = title;
this.body = body;
}
});
window.addEvent("domready", function() {
var Content = new Hash();
Content.set("dbitem15", new Article(1, "title1", "content1"));
Content.set("dbitem33", new Article(2, "title2", "content2"));
Hash.each(Content, function(value, key){
var article_title = new Element("div", {id: "title" + key});
article_title.set("class", "article_title");
article_title.innerHTML = value.title;
var article_content = new Element("div", {id: "content" + key});
article_content.set("class", "article_content");
article_content.innerHTML = value.body;
var article = new Element("div", {id: key});
article.set("class", "article");
article.inject(article_title, "after");
article.inject(article_content, "after");
//article.grab(article_title, "after");
//article.grab(article_content, "after");
//article.innerHTML = key;
$("plan").grab(article,"after");
});
});
How do I append a new Element into another? The code below is using the examples on the library's site but nothing is rendered. I tested the containing Element by using .innerHTML which does render. what am I doing wrong?
var Article = new Class({
initialize: function(order, title, body){
this.order = order;
this.title = title;
this.body = body;
}
});
window.addEvent("domready", function() {
var Content = new Hash();
Content.set("dbitem15", new Article(1, "title1", "content1"));
Content.set("dbitem33", new Article(2, "title2", "content2"));
Hash.each(Content, function(value, key){
var article_title = new Element("div", {id: "title" + key});
article_title.set("class", "article_title");
article_title.innerHTML = value.title;
var article_content = new Element("div", {id: "content" + key});
article_content.set("class", "article_content");
article_content.innerHTML = value.body;
var article = new Element("div", {id: key});
article.set("class", "article");
article.inject(article_title, "after");
article.inject(article_content, "after");
//article.grab(article_title, "after");
//article.grab(article_content, "after");
//article.innerHTML = key;
$("plan").grab(article,"after");
});
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
以下是一些有效的(重构的)代码: http://jsfiddle.net/mqchen/kJqLf/1/
主要区别在于:
问题是您使用了
inject(..., after)
这意味着您在article元素之后添加了创建的元素(标题、内容),这意味着当你稍后将文章元素放入“计划”元素中,未添加标题和内容元素(它们位于真空空间中的某处)。您需要使用grab(..., Bottom)
将标题和内容元素放入文章元素中。Here is some (refactored) code that works: http://jsfiddle.net/mqchen/kJqLf/1/
The main difference is this:
The problem was that you used
inject(..., after)
which means that you added the created elements (title, content) after the article element, which means that when you later put the article element into the "plan" element, the title and content elements were not added (they are somewhere in the empty vacuum of space). You need to put the title and content elements inside the article element usinggrab(..., bottom)
.假设我没有错误地读取代码,您正在尝试将
article_title
和article_content
插入到article
中,是吗?:在这种情况下,您已经改变了元素。
.inject()
引用要注入的元素,第一个参数是容器元素。所以尝试:
Assuming I am not reading the code incorrectly, you are attempting to insert the
article_title
andarticle_content
intoarticle
, yes?:In that case, you've switched the elements around.
.inject()
references the element to be injected, and the first parameter is for the container element.So try: