Mootools 1.3如何在“注入”功能中获得if语句?

时间:2011-06-22 02:56:02

标签: mootools inject

我需要在采用代码中使用以下if语句,但如果它存在则无效。有什么方法可以用其他方式做同样的事情吗?

var ele = new Element('li').adopt(

    new Element('span.title', {text:row.title}),
    new Element('span.subtitle').adopt(
        // Need this to work, but its invalid where it is atm
        if(row.subtitle = 1)
        {
            new Element('img', {src:'images/subTitle.png'})
        }
    ),
    new Element('span.bold', {text:row.bold}),
);

ele.iject(...

2 个答案:

答案 0 :(得分:1)

我对MooTools不太熟悉,但我不明白为什么这不起作用:

var subtitle = new Element('span.subtitle');
if (row.subtitle == 1) subtitle.adopt(new Element('img', {src:'images/subTitle.png'}));

var ele = new Element('li').adopt(
    new Element('span.title', {text:row.title}),
    subtitle,
    new Element('span.bold', {text:row.bold}),
);

答案 1 :(得分:1)

如果FTW立即:

var ele = new Element('li').adopt(

    new Element('span.title', {text:row.title}),
    new Element('span.subtitle').adopt(
        (row.subtitle == 1) ? new Element('img', {src:'images/subTitle.png'}) : null
    ),
    new Element('span.bold', {text:row.bold}),
);