User defined select. It is forbidden to select according to a certain condition, forming the solution of event linkage

During these two days, in the company's project, phtml files are maintained. There is a mutual constraint and user-defined select. It took a long time to solve the problem. Here, the element attribute textContent is mainly used.

  • We need to pay attention to
1> Sequence of events
 2> Staging of event content status
  • The code is as follows
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<style>
    body,
    p,
    ul,
    li {
        padding: 0;
        margin: 0;
    }

    li {
        list-style: none;
    }

    .list {
        margin: 50px;
    }

    .content {
        position: relative;
        width: 150px;
        height: 25px;
        border: 1px solid gray;
    }

    .content:after {
        content: '';
        position: absolute;
        right: 0;
        top: 0;
        height: 25px;
        width: 25px;
        background: gray;
    }

    .select-ul {
        display: none;
        width: 150px;
    }

    .select-ul li {
        width: 150px;
        border: 1px solid red;
    }

    .caret {
        position: absolute;
        top: 0;
        right: 0;
        width: 25px;
        height: 25px;
    }

    .caret:after {
        content: '';
        position: absolute;
        border-width: 5px;
        border-style: solid;
        left: 10px;
        top: 8px;
        z-index: 15;
        border-color: transparent transparent transparent red;
        transition: all 0.1s linear;
        transform: rotate(0deg);
    }

    .caret_after:after {
        border-width: 5px;
        border-style: solid;
        border-color: transparent transparent transparent red;
        transform: rotate(90deg);
    }
    .para{
        text-align:center;
        height:100%;
    }

    .restrict{
        background:black;
        display: inline-block;
        color:white;
        padding:5px;
        border:3px;
        cursor: pointer;
        margin-right:10px;
    }
    .active{
        background:lawngreen;

    }
</style>

<body>
    <div class="list">
        <span class="active restrict">5</span>
        <span class="restrict">10</span>
        <span class="restrict">15</span>
        <span class="restrict">20</span>
        <div class="content">
            <p class="para">0</p>
            <span class="caret"></span>
        </div>
        <ul class="select-ul">
            <li>1</li>
            <li>2</li>
            <li>3</li>
            <li>4</li>
            <li>5</li>
            <li>6</li>
            <li>7</li>
            <li>8</li>
            <li>9</li>
            <li>10</li>
            <li>11</li>
            <li>12</li>
            <li>13</li>
            <li>14</li>
            <li>15</li>
            <li>16</li>
            <li>17</li>
            <li>18</li>
            <li>19</li>
        </ul>
    </div>
</body>
<script>
    var oUl = document.getElementsByClassName("select-ul")[0];
    var oContent = document.getElementsByClassName("content")[0];
    var oCaret = document.getElementsByClassName("caret")[0];
    var oPara = document.getElementsByClassName("para")[0];
    var oParaText = '';
    // Get the default status value
    oPara.onclick = function(){
        oParaText = this.textContent
    }
    // Determine whether to display the custom drop-down box
    oContent.onclick = function () {
        if (oUl.style.display == "block") {
            oUl.style.display = "none";
            oCaret.classList.remove('caret_after');
            return;
        }
        oCaret.classList.add('caret_after');
        oUl.style.display = "block";
    }

    // Click on the elements in each list and assign values in the option box
    var oLi = document.getElementsByTagName("li");
    for (var i = 0; i < oLi.length; i++) {
        oLi[i].onclick = function () {
            if(Number(restrictSpanText) >= Number(this.textContent)){
                oPara.textContent = this.textContent;
            }
            else{
                oPara.textContent = oParaText;
                alert('The selected number is greater than the limit number,Please re select');
            }
            oContent.click();
        }
    }
    // Capture limiting elements 
    var oRestrict = document.getElementsByClassName("restrict");
    // Limit text, default values
    var restrictSpanText = oRestrict[0].textContent;
    // Add date limit event
    for(var i = 0; i< oRestrict.length;i++){
        oRestrict[i].onclick = function(){
            for(var j = 0 ; j < oRestrict.length;j++){
                if(Number(oPara.textContent)  > this.textContent){
                     alert("The selected limit number conflicts with the text box number, please re select");
                     return;
                }
                if(this!=oRestrict[j]){
                    oRestrict[j].classList.remove("active");
                }
                this.classList.add("active");
                restrictSpanText = this.textContent;
            }
        }
    }
</script>

</html>

If there is a better way, please point out that the project is make complaints about TM.

Tags: Attribute IE

Posted on Mon, 30 Mar 2020 13:27:46 -0700 by kevin99