RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 1131547
Accepted
Ann13
Ann13
Asked:2020-05-26 15:10:27 +0000 UTC2020-05-26 15:10:27 +0000 UTC 2020-05-26 15:10:27 +0000 UTC

如何不只用不同的类写五次相同的代码?

  • 772

在js的帮助下,我设计了一个下拉列表而不是选择。但是如果我使用 5 个列表,那么我将不得不使用不同的类编写 5 次 js 代码。

我认为这是不好的做法。

请告诉我如何为所有下拉列表创建一个函数?

function ready() {
    const select = document.querySelector('.select');
    const select_title = select.querySelector('.select_title');
    const select_labels = select.querySelectorAll('.select_label');

    select_title.addEventListener('click', () => {
        if ('active' === select.getAttribute('data-state')) {
            select.setAttribute('data-state', '');
        } else {
            select.setAttribute('data-state', 'active');
        }
    });

    // Close when click to option
    for (let i = 0; i < select_labels.length; i++) {
        select_labels[i].addEventListener('click', (evt) => {
            select_title.textContent = evt.target.textContent;
            select.setAttribute('data-state', '');
        });
    }
    
    ///close outside the element
    $(document).mouseup(function (e) {
        select.setAttribute('data-state', '');
    });
}
    document.addEventListener("DOMContentLoaded", ready);
.select {
    position: relative;
    width: 300px;
    height: 40px;
    margin: 20px 0;
}

.select[data-state="active"] .select_title::before {
    transform: translate(-3px, -50%) rotate(-45deg);
}

.select[data-state="active"] .select_title::after {
    transform: translate(3px, -50%) rotate(45deg);
}

.select[data-state="active"] .select_content {
    opacity: 1;
}

.select[data-state="active"] .select_label + .select_input + .select_label {
    max-height: 40px;
    border-top-width: 1px;
}

.select_title {
    display: flex;
    align-items: center;
    width: 100%;
    height: 100%;
    padding: 8px 16px;
    border-radius: 5px;
    border: solid 1px #c7ccd1;
    cursor: pointer;
    background-color: #38b0ff;
    color: #e9e9e9;
}

.select_title::before, .select_title::after {
    content: "";
    position: absolute;
    top: 50%;
    right: 16px;
    display: block;
    width: 10px;
    height: 2px;
    transition: all 0.3s ease-out;
    background-color: #e9e9e9;
    transform: translate(-3px, -50%) rotate(45deg);
}

.select_title::after {
    transform: translate(3px, -50%) rotate(-45deg);
}

.select_title:hover {
   color: #ffffff;
}

.select_title:hover::before, .select_title:hover::after {
    background-color: #f3fdff;
}

.select_content {
    position: absolute;
    top: 40px;
    left: 3px;
    display: flex;
    flex-direction: column;
    width: calc(100% - 6px);
    background-color: #ffffff;
    border: 1px solid #c7ccd1;
    border-top: none;
    border-bottom-left-radius: 5px;
    border-bottom-right-radius: 5px;
    transition: all 0.3s ease-out;
    opacity: 0;
    z-index: 8;
}

.select_input {
    display: none;
}

.select_input:checked + label {
    background-color: #dedede;
}

.select_input:disabled + label {
    opacity: 0.6;
    pointer-events: none;
}

.select_label {
    display: flex;
    align-items: center;
    width: 100%;
    height: 40px;
    max-height: 0;
    padding: 0 20px;
    transition: all 0.1s ease-out;
    cursor: pointer;
    overflow: hidden;

}

.select_label + input + .select_label {
    border-top: 0 solid #C7CCD160;
}

.select_label:hover {
    background-color: #38b0ff !important;
    color: #ffffff;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="select" data-state="">
    <div class="select_title type_request" value="" data-default="">Выберите тип</div>
    <div class="select_content">
        <input id="option0" class="select_input" value="" type="radio" name="singleSelect" />
        <label for="option0" class="select_label">Выберите тип</label>
        <input id="option1" class="select_input type_request" value="1" type="radio" name="singleSelect" />
        <label for="option1" class="select_label">Один</label>
        <input id="option2" class="select_input type_request" value="2" type="radio" name="singleSelect" />
        <label for="option2" class="select_label">Два</label>
        <input id="option3" class="select_input type_request" value="3" type="radio" name="singleSelect" />
        <label for="option3" class="select_label">Три</label>
        <input id="option4" class="select_input type_request" value="4" type="radio" name="singleSelect" />
        <label for="option4" class="select_label">Четыре</label>
    </div>
</div>

javascript
  • 1 1 个回答
  • 10 Views

1 个回答

  • Voted
  1. Best Answer
    InDevX
    2020-05-26T17:59:21Z2020-05-26T17:59:21Z

    或者,即时渲染 html。

    function createElement(items) {
      let elem = document.createElement("div");
      elem.classList.add('select');
      elem.dataset.state = "";
      addToggleEvent(elem);
     
      let title = document.createElement("div");
      title.classList.add('select_title','type_request');
      title.dataset.default = "";
      title.innerHTML = items[0].title;
     
      elem.append(title);
    
      let content = document.createElement("div");
      content.classList.add('select_content');
    
      items.forEach(function(item, idx) {
        let input = createInputItem(idx),
          label = createLabelItem(idx, item);
        content.append(input);
        content.append(label);
        addLabelEvent(label, title, elem);
      });
      
      elem.append(content);
    
      document.querySelector('body').append(elem);
    
      function createInputItem(idx) {
        let input = document.createElement("input");
        input.id = "option" + idx;
        input.classList.add('select_input');
        if (idx > 0) {
          input.classList.add('type_request');
        }
        input.value = idx == 0 ? "" : idx;
        input.type = "radio";
        input.name = "singleSelect";
        return input;
      }
    
      function createLabelItem(idx, item) {
        let label = document.createElement("label");
        label.for = "option" + idx;
        label.classList.add('select_label');
        label.innerHTML = item.title;
        return label;
      }
      
      function addToggleEvent(title){
        elem.addEventListener('click', e => {
      		elem.dataset.state = elem.dataset.state === 'active' ? '' : 'active';
      	});
      }
      
      function addLabelEvent(label, title, elem){
        label.addEventListener('click', e => {
          e.stopPropagation();
          title.textContent = e.target.textContent;
          elem.dataset.state = "";
        });
      }
      
      $(document).mouseup(function (e) {
        elem.dataset.state = "";
      });
    }
    
    createElement([
      { title: "1-5" },
      { title: "Один" },
      { title: "Два" },
      { title: "Три" },
      { title: "Четыре" },
      { title: "Пять" }
    ]);
    
    createElement([
      { title: "6-10" },
      { title: "Шесть" },
      { title: "Сумь" },
      { title: "Восемь" },
      { title: "Девять" },
      { title: "Десять" }
    ]);
    
    createElement([
      { title: "Да/Нет?" },
      { title: "Нет" },
      { title: "Да" }
    ]);
    .select {
        position: relative;
        width: 300px;
        height: 40px;
        margin: 20px 0;
    }
    
    .select[data-state="active"] .select_title::before {
        transform: translate(-3px, -50%) rotate(-45deg);
    }
    
    .select[data-state="active"] .select_title::after {
        transform: translate(3px, -50%) rotate(45deg);
    }
    
    .select[data-state="active"] .select_content {
        opacity: 1;
    }
    
    .select[data-state="active"] .select_label + .select_input + .select_label {
        max-height: 40px;
        border-top-width: 1px;
    }
    
    .select_title {
        display: flex;
        align-items: center;
        width: 100%;
        height: 100%;
        padding: 8px 16px;
        border-radius: 5px;
        border: solid 1px #c7ccd1;
        cursor: pointer;
        background-color: #38b0ff;
        color: #e9e9e9;
    }
    
    .select_title::before, .select_title::after {
        content: "";
        position: absolute;
        top: 50%;
        right: 16px;
        display: block;
        width: 10px;
        height: 2px;
        transition: all 0.3s ease-out;
        background-color: #e9e9e9;
        transform: translate(-3px, -50%) rotate(45deg);
    }
    
    .select_title::after {
        transform: translate(3px, -50%) rotate(-45deg);
    }
    
    .select_title:hover {
       color: #ffffff;
    }
    
    .select_title:hover::before, .select_title:hover::after {
        background-color: #f3fdff;
    }
    
    .select_content {
        position: absolute;
        top: 40px;
        left: 3px;
        display: flex;
        flex-direction: column;
        width: calc(100% - 6px);
        background-color: #ffffff;
        border: 1px solid #c7ccd1;
        border-top: none;
        border-bottom-left-radius: 5px;
        border-bottom-right-radius: 5px;
        transition: all 0.3s ease-out;
        opacity: 0;
        z-index: 8;
    }
    
    .select_input {
        display: none;
    }
    
    .select_input:checked + label {
        background-color: #dedede;
    }
    
    .select_input:disabled + label {
        opacity: 0.6;
        pointer-events: none;
    }
    
    .select_label {
        display: flex;
        align-items: center;
        width: 100%;
        height: 40px;
        max-height: 0;
        padding: 0 20px;
        transition: all 0.1s ease-out;
        cursor: pointer;
        overflow: hidden;
    
    }
    
    .select_label + input + .select_label {
        border-top: 0 solid #C7CCD160;
    }
    
    .select_label:hover {
        background-color: #38b0ff !important;
        color: #ffffff;
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

    upd.:如果您需要“传递”值,那么您可以例如将类/名称作为第二个参数传递给创建元素的函数,您可以通过它获取值,并添加此参数到相同的输入。

    更新。添加了 1 个 js 方法 + html 的选项...

    function ready() {
        const selects = document.querySelectorAll('.select');
        selects.forEach(function (select){
            const select_title = select.querySelector('.select_title');
            const select_labels = select.querySelectorAll('.select_label');
    
            select_title.addEventListener('click', () => {
                if ('active' === select.dataset.state) {
                    select.dataset.state = '';
                } else {
                    select.dataset.state = 'active';
                }
            });
    
            // Close when click to option
            for (let i = 0; i < select_labels.length; i++) {
                select_labels[i].addEventListener('click', (evt) => {
                    select_title.textContent = evt.target.textContent;
                    select.dataset.state = '';
                });
            }
    
            ///close outside the element
            window.addEventListener('mouseup', e => select.dataset.state = '');
        });
    }
    document.addEventListener("DOMContentLoaded", ready);
    .select {
        position: relative;
        width: 300px;
        height: 40px;
        margin: 20px 0;
    }
    
    .select[data-state="active"] .select_title::before {
        transform: translate(-3px, -50%) rotate(-45deg);
    }
    
    .select[data-state="active"] .select_title::after {
        transform: translate(3px, -50%) rotate(45deg);
    }
    
    .select[data-state="active"] .select_content {
        opacity: 1;
    }
    
    .select[data-state="active"] .select_label + .select_input + .select_label {
        max-height: 40px;
        border-top-width: 1px;
    }
    
    .select_title {
        display: flex;
        align-items: center;
        width: 100%;
        height: 100%;
        padding: 8px 16px;
        border-radius: 5px;
        border: solid 1px #c7ccd1;
        cursor: pointer;
        background-color: #38b0ff;
        color: #e9e9e9;
    }
    
    .select_title::before, .select_title::after {
        content: "";
        position: absolute;
        top: 50%;
        right: 16px;
        display: block;
        width: 10px;
        height: 2px;
        transition: all 0.3s ease-out;
        background-color: #e9e9e9;
        transform: translate(-3px, -50%) rotate(45deg);
    }
    
    .select_title::after {
        transform: translate(3px, -50%) rotate(-45deg);
    }
    
    .select_title:hover {
       color: #ffffff;
    }
    
    .select_title:hover::before, .select_title:hover::after {
        background-color: #f3fdff;
    }
    
    .select_content {
        position: absolute;
        top: 40px;
        left: 3px;
        display: flex;
        flex-direction: column;
        width: calc(100% - 6px);
        background-color: #ffffff;
        border: 1px solid #c7ccd1;
        border-top: none;
        border-bottom-left-radius: 5px;
        border-bottom-right-radius: 5px;
        transition: all 0.3s ease-out;
        opacity: 0;
        z-index: 8;
    }
    
    .select_input {
        display: none;
    }
    
    .select_input:checked + label {
        background-color: #dedede;
    }
    
    .select_input:disabled + label {
        opacity: 0.6;
        pointer-events: none;
    }
    
    .select_label {
        display: flex;
        align-items: center;
        width: 100%;
        height: 40px;
        max-height: 0;
        padding: 0 20px;
        transition: all 0.1s ease-out;
        cursor: pointer;
        overflow: hidden;
    
    }
    
    .select_label + input + .select_label {
        border-top: 0 solid #C7CCD160;
    }
    
    .select_label:hover {
        background-color: #38b0ff !important;
        color: #ffffff;
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <div class="select" data-state="">
        <div class="select_title type_request" value="" data-default="">1-4</div>
        <div class="select_content">
            <input id="option0" class="select_input" value="" type="radio" name="singleSelect" />
            <label for="option0" class="select_label">1-4</label>
            <input id="option1" class="select_input type_request" value="1" type="radio" name="singleSelect" />
            <label for="option1" class="select_label">Один</label>
            <input id="option2" class="select_input type_request" value="2" type="radio" name="singleSelect" />
            <label for="option2" class="select_label">Два</label>
            <input id="option3" class="select_input type_request" value="3" type="radio" name="singleSelect" />
            <label for="option3" class="select_label">Три</label>
            <input id="option4" class="select_input type_request" value="4" type="radio" name="singleSelect" />
            <label for="option4" class="select_label">Четыре</label>
        </div>
    </div>
    
    <div class="select" data-state="">
        <div class="select_title type_request" value="" data-default="">4-7</div>
        <div class="select_content">
            <input id="option0" class="select_input" value="" type="radio" name="singleSelect" />
            <label for="option0" class="select_label">4-7</label>
            <input id="option1" class="select_input type_request" value="1" type="radio" name="singleSelect" />
            <label for="option1" class="select_label">Четыре</label>
            <input id="option2" class="select_input type_request" value="2" type="radio" name="singleSelect" />
            <label for="option2" class="select_label">Пять</label>
            <input id="option3" class="select_input type_request" value="3" type="radio" name="singleSelect" />
            <label for="option3" class="select_label">Шесть</label>
            <input id="option4" class="select_input type_request" value="4" type="radio" name="singleSelect" />
            <label for="option4" class="select_label">Семь</label>
        </div>
    </div>
    
    <div class="select" data-state="">
        <div class="select_title type_request" value="" data-default="">Да/Нет?</div>
        <div class="select_content">
            <input id="option0" class="select_input" value="" type="radio" name="singleSelect" />
            <label for="option0" class="select_label">Да/Нет?</label>
            <input id="option0" class="select_input type_request" value="1" type="radio" name="singleSelect" />
            <label for="option0" class="select_label">Да</label>
            <input id="option1" class="select_input type_request" value="2" type="radio" name="singleSelect" />
            <label for="option1" class="select_label">Нет</label>
        </div>
    </div>

    • 1

相关问题

  • 第二个 Instagram 按钮的 CSS 属性

  • 由于模糊,内容不可见

  • 弹出队列。消息显示不正确

  • 是否可以在 for 循环中插入提示?

  • 如何将 JSON 请求中的信息输出到数据表 Vuetify vue.js?

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    如何从列表中打印最大元素(str 类型)的长度?

    • 2 个回答
  • Marko Smith

    如何在 PyQT5 中清除 QFrame 的内容

    • 1 个回答
  • Marko Smith

    如何将具有特定字符的字符串拆分为两个不同的列表?

    • 2 个回答
  • Marko Smith

    导航栏活动元素

    • 1 个回答
  • Marko Smith

    是否可以将文本放入数组中?[关闭]

    • 1 个回答
  • Marko Smith

    如何一次用多个分隔符拆分字符串?

    • 1 个回答
  • Marko Smith

    如何通过 ClassPath 创建 InputStream?

    • 2 个回答
  • Marko Smith

    在一个查询中连接多个表

    • 1 个回答
  • Marko Smith

    对列表列表中的所有值求和

    • 3 个回答
  • Marko Smith

    如何对齐 string.Format 中的列?

    • 1 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5