2017-01-29 8 views
0

以下のスニペットでは、行aにはクラスnew-entryと行cをクラスに追加してもらうことはできません。条件付きCSSのハンドルバーの使用

私はこれは愚かな間違いである確信しているが、私は

Handlebars.registerHelper("newEntry", function() { 
 
    return this.newEntry ? 'class="new-entry"' : ''; 
 
}); 
 

 
Handlebars.registerHelper("movingUp", function() { 
 
    return this.movingUp ? 'class="moving-up"' : ''; 
 
}); 
 

 
var source = document.getElementById('leaderboard-template').innerHTML; 
 
var template = Handlebars.compile(source); 
 
var outlet = document.getElementById('outlet'); 
 

 
    outlet.innerHTML = template({leaders: [ 
 
    {name: 'a', signature_count: 10, newEntry: true}, 
 
    {name: 'b', signature_count: 8}, 
 
    {name: 'c', signature_count: 6, movingUp: false}, 
 
    ]});
.new-entry { 
 
    background-color:red; 
 
} 
 
.moving-up { 
 
    color:red; 
 
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/4.0.6/handlebars.min.js"></script> 
 

 
<script id="leaderboard-template" type="text/x-handlebars-template"> 
 
    <table> 
 
     <thead> 
 
      <th>Constituency</th> 
 
      <th>Votes</th> 
 
     </thead> 
 
     <tbody> 
 
      {{#leaders}} 
 
      <tr {{newEntry}}> 
 
       <td>{{name}}</td> 
 
       <td><span {{movingUp}}>{{signature_count}}</span></td> 
 
      </tr> 
 
      {{/leaders}} 
 
     </tbody> 
 
    </table> 
 
</script> 
 

 
<div id="outlet"></div>

+0

私はstackoverflowのの 'の実行コードsnippet'ボタンをクリックして、ブラウザのF12ツールの出力を見てください。実際にはクラスが設定されていますが、すべての引用は '"' –

答えて

1

それを見ることができないハンドルは、HTMLへのヘルパーの戻り値は、文字列をエスケープ変換します。あなたはそれをしたくない場合は、このようなHandlebars.SafeStringを使用します。

Handlebars.registerHelper("newEntry", function() { 
    return new Handlebars.SafeString(this.newEntry ? 'class="new-entry"' : ''); 
}); 

Handlebars.registerHelper("movingUp", function() { 
    return new Handlebars.SafeString(this.movingUp ? 'class="moving-up"' : ''); 
}); 
関連する問題