html - 选项卡的 li 检查问题

标签 html css django tabs

大家好!我在 html 页面中添加了选项卡,用于在另一个页面中显示一个模型对象。

我的代码:

.tabordion {
    display: block;
    position: relative;
}

.tabordion input[name="sections"] {
    left: -9999px;
    position: absolute;
    top: -9999px;
}

.tabordion section {
    display: block;
}

.tabordion section label {
    border-bottom: 1px solid #696969;
    cursor: pointer;
    display: block;
    padding: 15px 20px;
    position: relative;
    width: 221px;
    z-index: 100;
}

.tabordion section article {
    display: none;
    left: 230px;
    min-width: 300px;
    padding: 0 0 0 21px;
    position: absolute;
    top: 0;
}

.tabordion section article:after {
    bottom: 0;
    content: "";
    display: block;
    left: -229px;
    position: absolute;
    top: 0;
    width: 220px;
    z-index: 1;
}

.tabordion input[name="sections"]:checked + label {
    font-weight: bold;
}

.tabordion input[name="sections"]:checked ~ article {
    display: block;
}
<div class="tabordion">
    <h4>Contents</h4>
    {% for subject in subjects %}
        {% if subject.book|safe == book.name|safe and subject.author|safe == user.username|safe %}
            <section id="section{{ subject.id }}">
                <input type="radio" name="sections" id="option{{ subject.id }}" checked>
                <label for="option{{ subject.id }}">{{ subject.name }}</label>
                <article>
                    {{ subject.content }}
                </article>
            </section>
        {% endif %}
    {% endfor %}
</div>

顺便说一下,我正在制作 django 元素。

问题是最后一部分首先 checkin 列表。这是垂直选项卡,我需要检查列表中的第一部分。我的代码有什么问题吗?

这是生成的代码:

<div class="tabordion">
        <section id="section1">
            <input type="radio" name="sections" id="option1" checked="">
            <label for="option1">New album</label>
            <article>
                Content
            </article>
        </section>



        <section id="section2">
            <input type="radio" name="sections" id="option2" checked="">
            <label for="option2">Name</label>
            <article>
                Content
            </article>
        </section>   
</div>

最佳答案

I need first section in list checked. What's wrong with my code?

将每个单选按钮标记为选中:

<input type="radio" name="sections" id="option{{ subject.id }}" checked>

...但是只能检查一组中的一个,因此最后一个获胜。相反,仅使用 if block 中的 forloop.first 标志在第一个单选输入上渲染 checked 属性:

<input type="radio" name="sections" id="option{{ subject.id }}" {% if forloop.first %}checked{% endif %}>

整个模板:

<div class="tabordion">
    <h4>Contents</h4>
    {% for subject in subjects %}
        {% if subject.book|safe == book.name|safe and subject.author|safe == user.username|safe %}
            <section id="section{{ subject.id }}">
                <input type="radio" name="sections" id="option{{ subject.id }}" {% if forloop.first %}checked{% endif %}>
                <label for="option{{ subject.id }}">{{ subject.name }}</label>
                <article>
                    {{ subject.content }}
                </article>
            </section>
        {% endif %}
    {% endfor %}
</div>

关于html - 选项卡的 li 检查问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32335751/

相关文章:

javascript - 如何在react中更改material-ui Textfield标签样式

javascript - 使用 Hammerjs 缩放图像

javascript - 使用 ng-class 在 AngularJS 中传递类名

python - 将具有多个循环和逻辑的代码简化为查询集聚合

html - 当 list-marker 是伪元素时,list-style-position 不起作用。为什么?

javascript - 如何在我网站的某个地方打开某个网站(例如 stackoverflow)?

php - Internet Explorer z-index 不适用于打开的 Flash 图表

CSS Percent size specifier sizing element to more than specified size

python - if 和 elif 在模板 django 中不起作用

python - 有什么办法django不保存用表单上传的文件