2011-06-22 61 views
0

我需要採用代碼中的以下if語句,但如果它在那裏則無效。有什麼辦法可以用其他方式做同樣的事情嗎?Mootools 1.3如何在「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(... 

回答

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}), 
); 
+0

乾杯:P應「這是一個想法」 – Nicekiwi

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}), 
);