javascript - jQuery 自动完成 - 没有任何样式?

标签 javascript jquery html css

下面是 jQuery 自动完成代码,它显示带有一些样式的结果 .如何删除所有样式属性,使其看起来像原始 html 下拉列表?如果不可能,是否可以在纯 JavaScript 中创建相同的自动完成效果?

现有代码:

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>jQuery UI Autocomplete - Combobox</title>
  <link rel="stylesheet" href="https://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
  <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
  <script src="https://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
  <style>
  .custom-combobox {
    position: relative;
    display: inline-block;
  }
  .custom-combobox-toggle {
    position: absolute;
    top: 0;
    bottom: 0;
    margin-left: -1px;
    padding: 0;
  }
  .custom-combobox-input {
    margin: 0;
    padding: 5px 10px;
  }
  </style>
  <script>
  function sendvalues()
  {
  alert();
  }
  (function( $ ) {
    $.widget( "custom.combobox", {
      _create: function() {
        this.wrapper = $( "<span>" )
          .addClass( "custom-combobox" )
          .insertAfter( this.element );
 
        this.element.hide();
        this._createAutocomplete();
        this._createShowAllButton();
      },
 
      _createAutocomplete: function() {
        var selected = this.element.children( ":selected" ),
          value = selected.val() ? selected.text() : "";
 
        this.input = $( "<input>" )
          .appendTo( this.wrapper )
          .val( value )
          .attr( "title", "" )
          .addClass( "custom-combobox-input ui-widget ui-widget-content ui-state-default ui-corner-left" )
          .autocomplete({
            delay: 0,
            minLength: 0,
            source: $.proxy( this, "_source" )
          })
          .tooltip({
            tooltipClass: "ui-state-highlight"
          });
 
        this._on( this.input, {
          autocompleteselect: function( event, ui ) {
            ui.item.option.selected = true;
            this._trigger( "select", event, {
			
              item: ui.item.option
            });
          },
 
          autocompletechange: "_removeIfInvalid"
        });
      },
 
      _createShowAllButton: function() {
        var input = this.input,
          wasOpen = false;
 
        $( "<a>" )
          .attr( "tabIndex", -1 )
          .attr( "title", "Show All Items" )
          .tooltip()
          .appendTo( this.wrapper )
          .button({
            icons: {
              primary: "ui-icon-triangle-1-s"
            },
            text: false
          })
          .removeClass( "ui-corner-all" )
          .addClass( "custom-combobox-toggle ui-corner-right" )
          .mousedown(function() {
            wasOpen = input.autocomplete( "widget" ).is( ":visible" );
          })
          .click(function() {
            input.focus();
 
            // Close if already visible
            if ( wasOpen ) {
              return;
            }
 
            // Pass empty string as value to search for, displaying all results
            input.autocomplete( "search", "" );
          });
      },
 
      _source: function( request, response ) {
        var matcher = new RegExp( $.ui.autocomplete.escapeRegex(request.term), "i" );
        response( this.element.children( "option" ).map(function() {
          var text = $( this ).text();
          if ( this.value && ( !request.term || matcher.test(text) ) )
            return {
              label: text,
              value: text,
              option: this
            };
        }) );
      },
 
      _removeIfInvalid: function( event, ui ) {
 
        // Selected an item, nothing to do
        if ( ui.item ) {
		sendvalues();
          return;
        }
 
        // Search for a match (case-insensitive)
        var value = this.input.val(),
          valueLowerCase = value.toLowerCase(),
          valid = false;
        this.element.children( "option" ).each(function() {
          if ( $( this ).text().toLowerCase() === valueLowerCase ) {
            this.selected = valid = true;
            return false;
          }
        });
 
        // Found a match, nothing to do
        if ( valid ) {
          return;
        }
 
        // Remove invalid value
        this.input
          .val( "" )
          .attr( "title", value + " didn't match any item" )
          .tooltip( "open" );
        this.element.val( "" );
        this._delay(function() {
          this.input.tooltip( "close" ).attr( "title", "" );
        }, 2500 );
        this.input.autocomplete( "instance" ).term = "";
      },
 
      _destroy: function() {
        this.wrapper.remove();
        this.element.show();
      }
    });
  })( jQuery );
 
  $(function() {
    $( "#combobox" ).combobox();
    $( "#toggle" ).click(function() {
      $( "#combobox" ).toggle();
    });
  });
  
 
  </script>
</head>
<body>
 
<div class="ui-widget">
 
  <select id="combobox" onchange="sendvalues()">
    <option value="">Select one...</option>
    <option value="abc">abc</option>
    <option value="kkk">kkk</option>
    <option value="sd">asf</option>
	<option value="abdfac">abasdfsac</option>
    <option value="kgfjkgkk">kkyk</option>
    <option value="sghkd">asgyjf</option>
	<option value="abyuic">abftfjvhjc</option>
    <option value="kkkgyjhk">kkgjk</option>
    <option value="sml,hud">asnjmf</option>
    
  </select>
</div>

 
</body>
</html>

当我刚刚删除 css 导入时,整个功能都发生了变化。该值显示在列表中的组合框下方,所选值单独显示,并且没有向下箭头。

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>jQuery UI Autocomplete - Combobox</title>
  
  <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
  <script src="https://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
  <style>
  .custom-combobox {
    position: relative;
    display: inline-block;
  }
  .custom-combobox-toggle {
    position: absolute;
    top: 0;
    bottom: 0;
    margin-left: -1px;
    padding: 0;
  }
  .custom-combobox-input {
    margin: 0;
    padding: 5px 10px;
  }
  </style>
  <script>
  function sendvalues()
  {
  alert();
  }
  (function( $ ) {
    $.widget( "custom.combobox", {
      _create: function() {
        this.wrapper = $( "<span>" )
          .addClass( "custom-combobox" )
          .insertAfter( this.element );
 
        this.element.hide();
        this._createAutocomplete();
        this._createShowAllButton();
      },
 
      _createAutocomplete: function() {
        var selected = this.element.children( ":selected" ),
          value = selected.val() ? selected.text() : "";
 
        this.input = $( "<input>" )
          .appendTo( this.wrapper )
          .val( value )
          .attr( "title", "" )
          .addClass( "custom-combobox-input ui-widget ui-widget-content ui-state-default ui-corner-left" )
          .autocomplete({
            delay: 0,
            minLength: 0,
            source: $.proxy( this, "_source" )
          })
          .tooltip({
            tooltipClass: "ui-state-highlight"
          });
 
        this._on( this.input, {
          autocompleteselect: function( event, ui ) {
            ui.item.option.selected = true;
            this._trigger( "select", event, {
			
              item: ui.item.option
            });
          },
 
          autocompletechange: "_removeIfInvalid"
        });
      },
 
      _createShowAllButton: function() {
        var input = this.input,
          wasOpen = false;
 
        $( "<a>" )
          .attr( "tabIndex", -1 )
          .attr( "title", "Show All Items" )
          .tooltip()
          .appendTo( this.wrapper )
          .button({
            icons: {
              primary: "ui-icon-triangle-1-s"
            },
            text: false
          })
          .removeClass( "ui-corner-all" )
          .addClass( "custom-combobox-toggle ui-corner-right" )
          .mousedown(function() {
            wasOpen = input.autocomplete( "widget" ).is( ":visible" );
          })
          .click(function() {
            input.focus();
 
            // Close if already visible
            if ( wasOpen ) {
              return;
            }
 
            // Pass empty string as value to search for, displaying all results
            input.autocomplete( "search", "" );
          });
      },
 
      _source: function( request, response ) {
        var matcher = new RegExp( $.ui.autocomplete.escapeRegex(request.term), "i" );
        response( this.element.children( "option" ).map(function() {
          var text = $( this ).text();
          if ( this.value && ( !request.term || matcher.test(text) ) )
            return {
              label: text,
              value: text,
              option: this
            };
        }) );
      },
 
      _removeIfInvalid: function( event, ui ) {
 
        // Selected an item, nothing to do
        if ( ui.item ) {
		sendvalues();
          return;
        }
 
        // Search for a match (case-insensitive)
        var value = this.input.val(),
          valueLowerCase = value.toLowerCase(),
          valid = false;
        this.element.children( "option" ).each(function() {
          if ( $( this ).text().toLowerCase() === valueLowerCase ) {
            this.selected = valid = true;
            return false;
          }
        });
 
        // Found a match, nothing to do
        if ( valid ) {
          return;
        }
 
        // Remove invalid value
        this.input
          .val( "" )
          .attr( "title", value + " didn't match any item" )
          .tooltip( "open" );
        this.element.val( "" );
        this._delay(function() {
          this.input.tooltip( "close" ).attr( "title", "" );
        }, 2500 );
        this.input.autocomplete( "instance" ).term = "";
      },
 
      _destroy: function() {
        this.wrapper.remove();
        this.element.show();
      }
    });
  })( jQuery );
 
  $(function() {
    $( "#combobox" ).combobox();
    $( "#toggle" ).click(function() {
      $( "#combobox" ).toggle();
    });
  });
  
 
  </script>
</head>
<body>
 
<div class="ui-widget">
 
  <select id="combobox" onchange="sendvalues()">
    <option value="">Select one...</option>
    <option value="abc">abc</option>
    <option value="kkk">kkk</option>
    <option value="sd">asf</option>
	<option value="abdfac">abasdfsac</option>
    <option value="kgfjkgkk">kkyk</option>
    <option value="sghkd">asgyjf</option>
	<option value="abyuic">abftfjvhjc</option>
    <option value="kkkgyjhk">kkgjk</option>
    <option value="sml,hud">asnjmf</option>
    
  </select>
</div>

 
</body>
</html>

最佳答案

您不能“删除所有样式,使其看起来像原始的 html 下拉菜单”。您必须添加或修改样式以使其看起来像您想要的那样。

当您删除 CSS 时它看起来不正确的原因是您正在使用复杂的插件来实现复杂的行为。如果您删除插件的任何部分(尤其是像 CSS 这样的重要部分),整个“机器”都会崩溃。

您需要做的是添加一些您自己的样式来覆盖插件的样式,或者更改插件使用的样式。

在我看来,它的外观与“自然”自动完成框的外观之间最大的区别在于该框具有浅灰色背景渐变。如果您右键单击该元素并选择“检查元素”,您将看到影响它的所有 CSS 规则。您只需要找到导致它具有该背景的规则。

在这种情况下,似乎任何具有 ui-state-default 类的元素都应该具有背景图像 images/ui-bg_glass_75_e6e6e6_1x400.png,并且规则来自 jquery-ui.css,第 866 行。因此您可以找到该 css 规则并修改它以满足您的需要,或者您可以引入您自己的样式表并覆盖此样式。

然后对您需要进行的每个小更改继续遵循该过程:检查元素;找到这样设计的规则;更改或覆盖规则。 (我会建议覆盖而不是根据一般原则进行更改,以减少您破坏插件的可能性)

希望这对您有所帮助。如果您需要进一步的帮助,请评论或编辑您的帖子。

关于javascript - jQuery 自动完成 - 没有任何样式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32766929/

相关文章:

javascript - 在移动设备和桌面设备之间切换悬停下拉菜单

jquery - CKEditor 的 SetData 在版本 4 中不起作用 -

php - 未设置 Cookie 但数据库已更改

css - 添加边框半径属性会更改边框的颜色

javascript - Firebase 权限被拒绝

javascript - typescript :类型 'string' 不可分配给类型 '"数字"| "2-digit"' 在 Date::toLocaleDateString()

javascript - jQuery滑动删除div栏

jquery - e.pageX 在所有分辨率下都无法正常工作

html - 带侧边栏的居中内容(无标题)

html - 带有纯CSS的边框图像