MooTools 使用抓取/注入来附加子元素

发布于 2024-12-07 06:27:53 字数 1179 浏览 0 评论 0原文

如何将一个新元素附加到另一个元素中?下面的代码使用了图书馆网站上的示例,但没有呈现任何内容。我使用渲染的 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

尘曦 2024-12-14 06:27:53

以下是一些有效的(重构的)代码: http://jsfiddle.net/mqchen/kJqLf/1/

主要区别在于:

article.grab(article_title, "bottom");
article.grab(article_content, "bottom");

问题是您使用了 inject(..., after) 这意味着您在article元素之后添加了创建的元素(标题、内容),这意味着当你稍后将文章元素放入“计划”元素中,未添加标题和内容元素(它们位于真空空间中的某处)。您需要使用grab(..., Bottom)将标题和内容元素放入文章元素中。

Here is some (refactored) code that works: http://jsfiddle.net/mqchen/kJqLf/1/

The main difference is this:

article.grab(article_title, "bottom");
article.grab(article_content, "bottom");

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 using grab(..., bottom).

(り薆情海 2024-12-14 06:27:53

假设我没有错误地读取代码,您正在尝试将 article_titlearticle_content 插入到 article 中,是吗?:

article.inject(article_title, "after");
article.inject(article_content, "after");

在这种情况下,您已经改变了元素。 .inject() 引用要注入的元素,第一个参数是容器元素。

所以尝试:

article_title.inject(article, "after");
article_content.inject(article, "after");

Assuming I am not reading the code incorrectly, you are attempting to insert the article_title and article_content into article, yes?:

article.inject(article_title, "after");
article.inject(article_content, "after");

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:

article_title.inject(article, "after");
article_content.inject(article, "after");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文