突出显示将 id 选择器传递给函数的特定 select2

我只需要突出显示#vans 而不是#cars

有时 #vans 可以是多个,有时也可以是非多个。但是我必须能够专门通过 ID 选择器来突出显示选择。下面是使用突出显示方法和使用 css 选择器答案突出显示 select2 的代码

<!DOCTYPE html>

<html>

<body>

  <script src="//ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>

  <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>

  <link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">

  <link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/css/select2.min.css" rel="stylesheet" />

  <script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/js/select2.min.js"></script>


  <select name="cars" class="mySelect" id="cars" multiple>

    <option value="volvo">Cars</option>

  </select>

  <select name="vans" class="mySelect" id="vans">

    <option value="volvo">Vans</option>

  </select>

  

  

  <script>

  function highlightSelect2(selector, isMultiple) {

  var isWhat = isMultiple ? '--multiple' : '__rendered'

  $('.select2-selection' + isWhat).effect("highlight", {

    color: '#f88'

  }, 10000);

}


$(document).ready(function() {

  //initilize select2

  $('.mySelect').select2();


  $('.mySelect').each(function(index, element) {

    let prop = $(element).prop('multiple')

    prop ? highlightSelect2("#vans",prop) : highlightSelect2("#vans")

  })

});

</script>

  

  

</body>


</html>


慕森王
浏览 64回答 1
1回答

摇曳的蔷薇

如果您知道只有select#vans需要突出显示的部分,则无需遍历所有 Select2 jQuery 项。此外,您highlightSelect2没有使用selector您传入的内容。使用您的代码示例,我对其进行了修改,以便只有该#vans元素将通过以下方式突出显示:不迭代所有select2元素(使用.each)这让您只能#vans直接突出显示,修改highlightSelect2以使用传入的selector删除isMultiple逻辑——没有必要<!DOCTYPE html><html><body>&nbsp; <script src="//ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>&nbsp; <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>&nbsp; <link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">&nbsp; <link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/css/select2.min.css" rel="stylesheet" />&nbsp; <script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/js/select2.min.js"></script>&nbsp; <select name="cars" class="mySelect" id="cars" multiple>&nbsp; &nbsp; <option value="volvo">Cars</option>&nbsp; </select>&nbsp; <select name="vans" class="mySelect" id="vans">&nbsp; &nbsp; <option value="volvo">Vans</option>&nbsp; </select>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <script>&nbsp; function highlightSelect2(selector) {&nbsp; &nbsp; $(selector)&nbsp; &nbsp; &nbsp; .next('.select2-container')&nbsp; &nbsp; &nbsp; .find(".select2-selection")&nbsp; &nbsp; &nbsp; .effect("highlight", {&nbsp; &nbsp; &nbsp; &nbsp; color: '#f88'&nbsp; &nbsp; &nbsp; }, 10000);&nbsp; }$(document).ready(function() {&nbsp; //initilize select2&nbsp; $('.mySelect').select2( { width: "25%" });&nbsp; // highlight the #vans select2&nbsp; highlightSelect2("#vans");});</script>&nbsp;&nbsp;&nbsp;&nbsp;</body>运行代码片段,您会看到它按照您对特定示例的预期运行。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript