Slidetoggle()函数无法正常工作

时间:2019-09-17 14:37:02

标签: javascript jquery html css

我是Web开发的初学者,并且在HTML5的选择选项功能中应用jquery函数slideToggle()时遇到问题。以下是我将slideToggle()函数与以下代码配合使用的代码:

//Script for select element

var x, i, j, selElmnt, a, b, c;
/*look for any elements with the class "custom-select":*/
x = document.getElementsByClassName("custom-select");
for (i = 0; i < x.length; i++) {
  selElmnt = x[i].getElementsByTagName("select")[0];
  /*for each element, create a new DIV that will act as the selected item:*/
  a = document.createElement("DIV");
  a.setAttribute("class", "select-selected");
  a.setAttribute("ID", "select-selected");
  a.innerHTML = selElmnt.options[selElmnt.selectedIndex].innerHTML;
  x[i].appendChild(a);
  /*for each element, create a new DIV that will contain the option list:*/
  b = document.createElement("DIV");
  b.setAttribute("class", "select-items select-hide");
  b.setAttribute("ID", "selectItems");
  for (j = 1; j < selElmnt.length; j++) {
    /*for each option in the original select element,
    create a new DIV that will act as an option item:*/
    c = document.createElement("DIV");
    c.innerHTML = selElmnt.options[j].innerHTML;
    c.addEventListener("click", function(e) {
        /*when an item is clicked, update the original select box,
        and the selected item:*/
        var y, i, k, s, h;
        s = this.parentNode.parentNode.getElementsByTagName("select")[0];
        h = this.parentNode.previousSibling;
        for (i = 0; i < s.length; i++) {
          if (s.options[i].innerHTML == this.innerHTML) {
            s.selectedIndex = i;
            h.innerHTML = this.innerHTML;
            y = this.parentNode.getElementsByClassName("same-as-selected");
            for (k = 0; k < y.length; k++) {
              y[k].removeAttribute("class");
            }
            this.setAttribute("class", "same-as-selected");
            break;
          }
        }
        h.click();
    });
    b.appendChild(c);
  }
  x[i].appendChild(b);
  a.addEventListener("click", function(e) {
      /*when the select box is clicked, close any other select boxes,
      and open/close the current select box:*/
      e.stopPropagation();
      closeAllSelect(this);
      this.nextSibling.classList.toggle("select-hide");
      this.classList.toggle("select-arrow-active");
    });
}
function closeAllSelect(elmnt) {
  /*a function that will close all select boxes in the document,
  except the current select box:*/
  var x, y, i, arrNo = [];
  x = document.getElementsByClassName("select-items");
  y = document.getElementsByClassName("select-selected");
  for (i = 0; i < y.length; i++) {
    if (elmnt == y[i]) {
      arrNo.push(i)
    } else {
      y[i].classList.remove("select-arrow-active");
    }
  }
  for (i = 0; i < x.length; i++) {
    if (arrNo.indexOf(i)) {
      x[i].classList.add("select-hide");
    }
  }
}
/*if the user clicks anywhere outside the select box,
then close all select boxes:*/

document.addEventListener("click", closeAllSelect);

//select dropdown slide function
$(document).ready(function(){
  $("#select-selected").click(function(){
    $("#selectItems").slideToggle("slow");
  });
});
/*the container must be positioned relative:*/
.custom-select {
  position: relative;
  font-family: myTrench
}

.custom-select select {
  display: none; /*hide original SELECT element:*/
}

/*hide default select in IE8*/
select::-ms-expand {
    display: none;
}

/*style the arrow inside the select element:*/
.select-selected:after {
  position: absolute;
  content: "";
  top: 40px;
  right: 5px;
  width: 0;
  height: 0;
  border: 10px solid transparent;
  border-color: #212121 transparent transparent transparent;
}

/*point the arrow upwards when the select box is open (active):*/
.select-selected.select-arrow-active:after {
  border-color: transparent transparent #212121 transparent;
  top: 20px;
}

/*style the items (options), including the selected item:*/
.select-items div,.select-selected {
  color: #212121;
  padding: 30px 16px;
  border: 1px solid transparent;
  border-color: transparent transparent rgba(0, 0, 0, 0.1) transparent;
  cursor: pointer;
  user-select: none;
  font-size: 20px;
  height: 5px
  }

.select-selected {
	background-color: #06FFFB;
	height: 30px;
	font-size: 22px;
	font-weight: 900;
	color: #212121
	}


/*style items (options):*/
.select-items {
  position: absolute;
  background-color: #06FFFB;
  top: 100%;
  left: 0;
  right: 0;
  z-index: 99;
}

/*hide the items when the select box is closed:*/
.select-hide {
  display: none;
}

.select-items div:hover, .same-as-selected {
  background-color: #85FCFF;
}
<div style="position: absolute; left: 35%; top: 5%">
<span>&nbsp; Choose Income Tax Rate:</span><br><br>
<div class="custom-select" style="width:400px">
<select id="itrate">
<option value="0">Choose income tax rate as per your requirement...</option>
<option value=".03">3% -Transportation, IT services etc...</option>
<option value=".08">8% -Services|Companies|Filer</option>
<option value=".1">10% -Services|Individuals|Filer</option>
<option value=".07">7% -Contract Services|Companies|Filer</option>
<option value=".075">7.5% -Contract Services|Individuals|Filer</option>

</select>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

问题是,当我第一次单击“选择选项” div时,它会产生非常难看的幻灯片效果。另外,根据此脚本,当用户单击div以外的任何位置时,选择选项菜单应自动隐藏,并且在应用此“ slideToggle()”函数后不会发生这种情况。

PS:我从w3schools网站复制了样式代码(css + javascript)。我只在其javascript代码中添加了一个ID,并尝试在其上应用slideToggle()效果。请指导我在哪里出错了?

0 个答案:

没有答案