You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

星级评分题型Div未与其他同类题型居中对齐样式异常

问题原因

星级题样式失效核心是生成的HTML结构和其他题型不统一,属于基础模板疏漏,具体问题点:

  • 写newStarQuestion函数时,平白多套了一层<table><tr><td>标签把.starQuestion容器包在里面,还在题型里嵌了一层多余的<form>——你页面外层本来就有个form包着整个问卷区,嵌套表单是不符合HTML规范的,会触发异常渲染。之前给所有题型配的边框、边距、居中样式都是绑定在.opQuestion/.shAnQuestion/.numQuestion/.starQuestion这些类上的,外层无样式的表格会阻断样式生效,自然和其他题型显示效果不一样。
  • 星级题的label标签上错加了readonly属性,这个属性是给输入框用的,加在label上完全是无效代码。
  • 星级样式写的不全:没隐藏原生的单选框、没给星星设排列规则、选中高亮的选择器逻辑写反了,而且没加载图标库的话,你写的星星字符码根本显示不出来。
  • 顺带提个逻辑bug:每次新建题目你都要把页面上所有删除按钮查一遍重新绑事件,点删除的时候会重复触发移除逻辑,不影响样式但属于冗余代码。
修复步骤

1. 修正星级题生成模板

把newStarQuestion里多余的表格、嵌套form全删掉,去掉label上的无效readonly属性,让.starQuestion直接作为根元素插进去,和其他三个题型的结构保持一致:

function newStarQuestion() {
    questionnaire.insertAdjacentHTML('beforeend',
        `<div class="starQuestion">
        <div class="questionName" contenteditable="true">Your Question</div> 
        <button class="remove-qu" type="button">remove Question</button> 
        <div class="numbers">
        <input class="star star-5" id="star-5" type="radio" name="star" disabled/>
        <label class="star star-5" for="star-5"> </label>
        <input class="star star-4" id="star-4" type="radio" name="star" disabled/>
        <label class="star star-4" for="star-4"> </label>
        <input class="star star-3" id="star-3" type="radio" name="star" disabled/>
        <label class="star star-3" for="star-3"> </label>
        <input class="star star-2" id="star-2" type="radio" name="star" disabled/>
        <label class="star star-2" for="star-2"> </label>
        <input class="star star-1" id="star-1" type="radio" name="star" disabled/>
        <label class="star star-1" for="star-1"> </label>
         </div> </div>`);

    // 嫌每次遍历麻烦可以直接改事件委托,给questionaire绑一次点击事件就行,不用每次新建都重绑
    document.querySelectorAll('.remove-qu').forEach((item) => {
        item.onclick = function () { this.parentNode.remove(); }
    })
}

2. 补全星级相关CSS

把你之前写的不完整的星级样式替换成下面的代码,保证星星排列、选中效果正常:

/* 隐藏浏览器默认的单选按钮 */
input.star {
    display: none;
}
/* 星级区域用反向弹性布局,实现选5星全亮的效果 */
.starQuestion .numbers {
    display: flex;
    flex-direction: row-reverse;
    justify-content: center;
    gap: 8px;
    margin-bottom: 25px;
    font-size: 30px;
}
/* 星星默认样式 */
label.star {
    cursor: pointer;
    color: #ccc;
}
/* 选中、鼠标悬浮时星星变成实心高亮 */
input.star:checked ~ label.star:before,
label.star:hover ~ label.star:before,
label.star:hover:before {
    content: '\f005';
    color: #ffc107;
    transition: all .25s;
}
/* 未选中状态是空心星 */
label.star:before {
    content: '\f006';
    font-family: FontAwesome;
}

3. 加载图标依赖

在页面头部引入FontAwesome图标库,不然你写的星星字符编码没法渲染成对应的图标。

改完之后星级题的容器结构和其他题型完全一致,之前配的统一边框、居中、边距样式会直接生效,不会再出现显示异常的问题。

内容的提问来源于stack exchange,提问作者Chris Costa

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.28 09:18:17