Mootools元素注入

发布于 2024-12-12 19:17:13 字数 317 浏览 3 评论 0原文

很抱歉,如果这已经被涵盖了,我已经看过了,但没有遇到问题 - 答案(我过去在这里找到了我的问题的很多答案 - 继续努力,伙计们+女孩们)

我学习 Mootools 元素注入,我使用它的时间并不长,但我发现它让我对各种用法大开眼界。我的问题是如何构建链接到 url 的新图像元素?例如我如何注射;

<a href="[url]"><img src="[path]" /></a>

我可以将其作为 2 个独立的元素来完成但努力将它们结合为一个元素。感谢您阅读我的问题并提供任何建议。

标记

im sorry if this has already been covered, i have had a look but didn't come acrross a question - answer ( I have found so many answers to my question on here in the past - keep up the good work guys + gals )

Im learning Mootools element injection, I have not been using this for long and I am finding it is opening my eyes to all sorts of usages. My question is how do I construct a new image element that is linked to a url? for example how do I inject;

<a href="[url]"><img src="[path]" /></a>

I can do this as 2 seperate elements & but struggling to combine them as one element. thank you for reading my question and for any advice given.

Mark

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

醉城メ夜风 2024-12-19 19:17:14

感谢您的回复,我一直在尝试并根据我的需求提出了以下解决方案

photos.each(function(photo){
    var el = new Element('div.image'),
    image = new Element('img', {
        'src': photo.src 
    }).inject(el),
    defaultLink = new Element( 'a', {
        'class': 'makeImageDefault', 'name': photo.id
    }).inject(image, 'after'),
    defaultImageIcon = new Element( 'img', { 
        'src': photo.defaultIcon 
    }).inject(defaultLink, 'inside' )
    el.inject($('photoGrid'));
});

thanks for your responses, ive been experimenting and have come up with the following solution for my needs

photos.each(function(photo){
    var el = new Element('div.image'),
    image = new Element('img', {
        'src': photo.src 
    }).inject(el),
    defaultLink = new Element( 'a', {
        'class': 'makeImageDefault', 'name': photo.id
    }).inject(image, 'after'),
    defaultImageIcon = new Element( 'img', { 
        'src': photo.defaultIcon 
    }).inject(defaultLink, 'inside' )
    el.inject($('photoGrid'));
});
神仙妹妹 2024-12-19 19:17:13

使用 Elements.from,它快速、完全有效且简单。

var collection = '<a href="{href}"><img src="{src}" /></a>';
Elements.from(collection.substitute({
    href: 'http://www.stackoverflow.com',
    src: 'http://www.placekitten.com/300/300'
})).inject(document.body);

一个明显的缺点是您没有对创建的节点的引用,只有元素集合的引用。

Use Elements.from, it's fast, perfectly valid, and simple.

var collection = '<a href="{href}"><img src="{src}" /></a>';
Elements.from(collection.substitute({
    href: 'http://www.stackoverflow.com',
    src: 'http://www.placekitten.com/300/300'
})).inject(document.body);

An obvious disadvantage is that you have no references to the nodes created, only to the collection of elements.

亚希 2024-12-19 19:17:13

您可以使用 grab注入 到两个新元素(imga) 来嵌套它们:

var anchor = new Element("a", {
    href: "http://www.stackoverflow.com"
});

var img = new Element("img", {
    src: "http://www.placekitten.com/300/300"
});

// inject the img into the anchor
img.inject(anchor);

// Append the anchor into the body:
document.body.grab(anchor);

这将生成以下 HTML:

<a href="http://www.stackoverflow.com">
    <img src="http://www.placekitten.com/300/300">
</a>

示例: http://jsfiddle.net/tNamz/

You could use grab or inject on two new elements (an img and a) to nest them:

var anchor = new Element("a", {
    href: "http://www.stackoverflow.com"
});

var img = new Element("img", {
    src: "http://www.placekitten.com/300/300"
});

// inject the img into the anchor
img.inject(anchor);

// Append the anchor into the body:
document.body.grab(anchor);

This will generate the following HTML:

<a href="http://www.stackoverflow.com">
    <img src="http://www.placekitten.com/300/300">
</a>

Example: http://jsfiddle.net/tNamz/

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