If statement JsRender if condition在自定义范围标记内失败
我正在尝试将选定的类添加到范围标记内的img标记中If statement JsRender if condition在自定义范围标记内失败,if-statement,jsrender,If Statement,Jsrender,我正在尝试将选定的类添加到范围标记内的img标记中 <ul class="cci-TweetContainer"> {{range refTweets start=0 end=9}} <li data-userID="{{:userId}}"> <a href="javascript:void(0);" alt="User Avatar" data-userID="{{:userId}}"> {{if refTwe
<ul class="cci-TweetContainer">
{{range refTweets start=0 end=9}}
<li data-userID="{{:userId}}">
<a href="javascript:void(0);" alt="User Avatar" data-userID="{{:userId}}">
{{if refTweets == 0}}
<img class="selected" src="{{:userAvatar}}" alt="User avatar icon" width="25" height="25" />
{{else}}
<img src="{{:userAvatar}}" alt="User avatar icon" width="25" height="25" />
{{/if}}
</a>
</li>
{{/range}}
</ul>
{{range refTweets start=0 end=9}
-
{{/range}}
refTweets是数组吗?如果是,那么在{range}内,当前数据对象将是refTweets数组中的当前项。因此测试refTweets==0
似乎没有意义。实际上,您正在测试当前对象上的refTweets属性-因此refTweet.refTweets==0
-该属性将始终为false
您可以像这样在上下文中传递:
{{range refTweets~selId=selectedId start=0 end=9}
{{/range}}
(假设您在具有refTweets属性的同一对象上具有selectedId属性)。通过添加引用refTweets属性的#getIndex(#parent)使代码正常工作
{{range refTweets start=0 end=9}}
<li data-userID="{{:userId}}">
<a href="javascript:void(0);" alt="User Avatar" data-userID="{{:userId}}">
{{if #getIndex(#parent) == 0}}
<img class="selected" src="{{:userAvatar}}" alt="User avatar icon" width="25" height="25" />
{{else}}
<img src="{{:userAvatar}}" alt="User avatar icon" width="25" height="25" />
{{/if}}
</a>
</li>
{{/range}}
{{range refTweets start=0 end=9}
{{/range}}
谢谢Boris,我同意你的解决方案,但通过在if条件下查询#getIndex(#parent)==0,我得到了一个更简单的解决方案。实际上,getIndex()不带参数,因此传入#parent并没有任何作用。这相当于写{{if!#getIndex()}}。事实上,您甚至可以只写{{if!#index},只需选择列表中的第一项即可。(你的问题不清楚这就是你想要的…)对不起,下次我会尽量说得更具体一些。但是谢谢你花时间回答我的问题。