【问题标题】:Change placeholder on link click in html在html中单击链接更改占位符
【发布时间】:2019-07-31 09:08:06
【问题描述】:

使用下面的代码,我试图在点击链接时将placeholder 更改为select 元素。但目前没有看到任何变化。此外,我试图弄清楚单击齿轮图标时如何显示div。目前它在点击div 时有效,但不适用于其中的图标。

<!doctype html>
<html lang="en">
<head>
<!-- Dropdown - > https://stackoverflow.com/questions/43579748/how-to-achieve-autocomplete-feature-over-html-drop-down-or-select-element -->
<!-- focus in-out event > https://stackoverflow.com/questions/57284729/onclick-change-width-of-dropdown-using-javascript/57284975#57284975 -->
<title>jQuery UI Autocomplete - Default functionality</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/css/select2.css" rel="stylesheet" />
<style>
.dropbtn {
  background-color: #3498DB;
  color: white;
  padding: 8px;
  font-size: 16px;
  border: none;
  cursor: pointer;
}

.dropbtn:hover, .dropbtn:focus {
  background-color: #2980B9;
}

.dropdown {
  position: relative;
  display: inline-block;
}

.dropdown-content {
  display: none;
  position: absolute;
  background-color: #f1f1f1;
  min-width: 160px;
  overflow: auto;
  box-shadow: 0px 8px 8px 0px rgba(0,0,0,0.2);
  z-index: 1;
}

.dropdown-content a {
  color: black;
  padding: 8px 8px;
  text-decoration: none;
  display: block;
}

.dropdown a:hover {background-color: #ddd;}

.show {display: block;}
</style>
<script>
$(function() {
  $('select')
    .select2({
      placeholder: 'Search Command...',
      width: '200',
      multiple: false,
      data: [{
        id: '',
        text: ''
      }, {
        id: 'rrrrrrr',
        text: 'testing1'
      }, {
        id: 'testing 1,2,3',
        text: 'testing 1,2,3gffffff'
      }],
      tokenSeparators: ['|']
    })
    $('.select2-container').click(function() {
      $(this).css('width','500px');
    });    
    $('.select2-container').focusout(function() {
      $(this).css('width','200px');
    });
    $('#changeCommand').click(function() {
      $('select').css('placeholder','Search Command...');
    });    
    $('#changePref').click(function() {
      $('select').css('placeholder','Search Preferences...');
    });
    $('#changeCD').click(function() {
      $('select').css('placeholder','Search Customer Default...');
    });    
});

</script>
<script>
<!-- https://www.w3schools.com/howto/tryit.asp?filename=tryhow_css_js_dropdown -->
/* When the user clicks on the button, 
toggle between hiding and showing the dropdown content */
function myFunction() {
  document.getElementById("myDropdown").classList.toggle("show");
}

// Close the dropdown if the user clicks outside of it
window.onclick = function(event) {
  if (!event.target.matches('.dropbtn')) {
    var dropdowns = document.getElementsByClassName("dropdown-content");
    var i;
    for (i = 0; i < dropdowns.length; i++) {
      var openDropdown = dropdowns[i];
      if (openDropdown.classList.contains('show')) {
        openDropdown.classList.remove('show');
      }
    }
  }
}
</script>
</head>
<body>
    <select></select> <div class="dropdown"><button class="dropbtn" onclick="myFunction()" style="border-radius: 3px;border: none;color: black; background-color: white;"><span>&#9881;</span></button></div>
  <div id="myDropdown" class="dropdown-content">
    <a id="changeCommand">Commands</a>
    <a id="changePref">Preferences</a>
    <a id="changeCD">Customer Default</a>
  </div>    
</body>
</html>

【问题讨论】:

  • 你可以用 attr api.jquery.com/attr w3schools.com/jquery/html_attr.asp 替换和尝试 .css
  • 试试这样$('select').attr('placeholder','your text...');
  • 谢谢,但没用。
  • placeholder 是 HTML 属性,而不是 CSS 属性。
  • @Experimenter 您只想更改占位符标签吗? select2 数据选项呢?它们是始终保持不变还是您必须使用其他数据重新初始化插件?

标签: javascript jquery html jquery-select2


【解决方案1】:

您可以尝试以下方法:

$(function() {
  $('select')
    .select2({
      placeholder: 'Search Command...',
      allowClear: true,
      width: '200',
      multiple: false,
      data: [{
        id: '',
        text: ''
      }, {
        id: 'rrrrrrr',
        text: 'testing1'
      }, {
        id: 'testing 1,2,3',
        text: 'testing 1,2,3gffffff'
      }],
      tokenSeparators: ['|']
    })
    .on('select2:open', function() {
      $('.select2-container').css('width','600px');
    })
    .on("select2:close", function () { 
      $('.select2-container').css('width','200px');
    });
    $('#changeCommand').click(function() {
      $('.select2-selection__placeholder').text('Search Command...');
    });    
    $('#changePref').click(function() {
      $('.select2-selection__placeholder').text('Search Preferences...');
    });
    $('#changeCD').click(function() {
      $('.select2-selection__placeholder').text('Search Customer Default...');
    });    
});

// Close the dropdown if the user clicks outside of it
window.onclick = function(event) {
  var dropdowns = document.getElementById("myDropdown");
  if (event.target.classList.contains('dropbtn')) {  
    dropdowns.classList.toggle("show");    
  }
  else if (!event.target.classList.contains("dropbtn") && dropdowns.classList.contains("show")){
    dropdowns.classList.toggle("show");
  }
}
.dropbtn {
  background-color: #3498DB;
  color: white;
  padding: 8px;
  font-size: 16px;
  border: none;
  cursor: pointer;
}

.dropbtn:hover, .dropbtn:focus {
  background-color: #2980B9;
}

.dropdown {
  position: relative;
  display: inline-block;
}

.dropdown-content {
  display: none;
  position: absolute;
  background-color: #f1f1f1;
  min-width: 160px;
  overflow: auto;
  box-shadow: 0px 8px 8px 0px rgba(0,0,0,0.2);
  z-index: 1;
}

.dropdown-content a {
  color: black;
  padding: 8px 8px;
  text-decoration: none;
  display: block;
}

.dropdown a:hover {background-color: #ddd;}

.show {display: block;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/css/select2.css" rel="stylesheet" />

<select></select> 
<div class="dropdown"><button class="dropbtn" style="border-radius: 3px;border: none;color: black; background-color: white;">&#9881;</button></div>
<div id="myDropdown" class="dropdown-content">
  <a id="changeCommand">Commands</a>
  <a id="changePref">Preferences</a>
  <a id="changeCD">Customer Default</a>
</div>

【讨论】:

  • @Experimenter,你的代码有几个问题,上面的解决方案是我认为最接近的:)
【解决方案2】:

只需删除 window.onclick 事件上的订阅即可。 单击子元素时将触发按钮上的 onclick 事件。您当前的逻辑将显示并直接隐藏下拉菜单

【讨论】:

  • 是的,但是当点击空白区域的任何地方时,这不会折叠下拉菜单。
  • 由于您已经在使用 jQuery,我会将事件绑定到文档并检查是否单击了具有类 drop-btn 的元素。如果是这样,切换下拉元素上的显示类,否则隐藏下拉元素。你可以在这里查看:jsfiddle.net/70j5deat/4
猜你喜欢
  • 2014-04-02
  • 2020-01-02
  • 2021-05-08
  • 1970-01-01
  • 2017-01-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-04
相关资源
最近更新 更多