为什么这里没有应用来自模板dom的html

Why does the html from template dom-if not get applied here

本文关键字:dom html 这里 应用 为什么      更新时间:2023-09-26

考虑以下代码:

<div class="container">
    <table class="c">
        <tr>
        <template is="dom-repeat" items="[[days]]" as="o">
            <td class$="[[dayClass(o)]]"><span class="d">[[formatDay(o)]]</span></td>
            <template is="dom-if" if="[[lastDayOfWeek(o)]]"></tr>
                <template is="dom-if" if="[[!lastDayOfCalendar(o)]]"><tr></template>
            </template>
        </template>
        </tr>
    </table>
</div>

为什么</tr><tr>没有应用于html?

完整的JS Bin演示在这里:https://jsbin.com/dujekefoga/edit?html,输出

编辑:固定JS Bin url

您的表行标记放错了位置,通过更改标记的放置顺序,我更接近您的预期结果。希望这对你有帮助。

https://jsbin.com/quvutugijo/1/edit?html,输出

最后,这就是工作原理。我相信这是最好的解决方案,因为它使用两个非嵌套的dom-if,并且html是有效的。

我只需要添加一个函数firstDayOfWeek,如果item.isoWeekday() === 1,它将返回true。

    <template is="dom-repeat" items="[[days]]" as="o">
        <template is="dom-if" if="[[firstDayOfWeek(o)]]"><tr></template>
        <td class$="[[dayClass(o)]]"><span class="d">[[formatDay(o)]]</span></td>
        <template is="dom-if" if="[[lastDayOfWeek(o)]]"></tr></template>
    </template>