jQuery UI - 自动完成 - 根据内容动态设置
有没有办法根据返回的文本长度动态设置自动完成对象的宽度?
谢谢
Is there a way to set the width of an autocomplete object dynamically, based on the length of text returned to it?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这需要一些努力,但我相信我有一个合理的解决方案。这主要需要绑定到自动完成小部件的“打开”事件,以便更改菜单显示时的默认行为,并修改一些 CSS 属性来帮助我们解决这个问题。
该方法有一个警告:这将导致背景栏仅跨越文本的长度,而不是一直跨越 IE7 中的菜单。它将在 IE8+ 以及所有其他主要浏览器中正常运行。我不鼓励您将其视为一个问题,因为您将针对不到 3% 的市场(甚至可能更少的实际收视率)来定制解决方案以适应视觉差异。
CSS
将这些样式添加到您的项目中以帮助视觉显示。 .ui-autocomplete 样式是创建可滚动菜单项列表的相当标准的样式,您可能已经在使用它了。应用于锚点的正确填充是为了在 Firefox 和 Webkit 浏览器中为滚动条留出适当的空间。它比直接应用于 UL 容器效果更好。
jQuery
绑定到 open 事件的处理程序的作用非常明显。首先,它使用的选择器查找唯一可见的自动完成菜单。值得庆幸的是,只有在显示自动完成菜单后才会引发 open 事件。如果页面上有多个自动完成小部件,这将帮助您的处理程序引导其注意力。从那里,它删除了直接应用于无序列表 CSS 的“宽度”属性。然后,它找到列表中的所有锚点后代,并用不间断空格对其 HTML 中的所有空白进行全局替换。 nbsp 强制锚点通过增加其宽度而不是将文本换行来进行响应,从而增加整个菜单的宽度。
您可以在此处查看工作演示。我建议输入“ab”,然后输入“deep”,以查看调整大小时发生的差异。抱歉,代码中的颜色列表很长(sorce),但我想给你有机会获得一系列可能的建议来查看调整大小的差异。
编辑
不知道我当时在想什么,但更简单的方法是提供以下附加样式并放弃打开事件处理程序:
This one took a bit of effort but I believe I have a reasonable solution. Primarily this requires binding to the "open" event of the autocomplete widget in order to change the default behavior of the menu as it displays, as well as modifying a few CSS attributes to help us out.
The method comes with one caveat: this will cause the background bars to only span the length of the text rather than all the way across the menu in IE7. It will function normally in IE8+ as well as all other major browsers. I would discourage you from treating it as an issue, as you'd be tailoring your solution to a visual disparity for less than 3% of the market and probably even less of your actual viewership.
The CSS
Add these styles to your project to help with visual display. The .ui-autocomplete styles are fairly standard to create a scrollable list of menu items and you are likely already using it. The right padding applied to the anchor is to allow for the proper amount of room for a scrollbar in Firefox and Webkit browsers. It works better than applying it directly to the UL container.
The jQuery
The handler bound to the open event is mostly obvious in what it does. First, the selector it uses looks for the only autocomplete menu visible. Thankfully, the open event is raised only after the autocomplete menu has been shown. This will help your handler direct its attention in the event you have more than one autocomplete widget on a page. From there, it removes the "width" attribute that's applied directly to the CSS of the unordered list. Then, it finds all anchor descendants in the list and does a global replace on all white space in its HTML with non-breaking spaces. The nbsp forces the anchor to respond by increasing its width rather than wrapping text to a new line, thereby increasing the width of the entire menu.
You can see a working demo here. I'd suggest typing in "ab" followed by "deep" to see the differences in resizing as they happen. Sorry for the long list of colors (sorce) in the code, but I wanted to give you the opportunity to get a good range of possible suggestions to view differences in the resizing.
EDIT
Don't know what I was thinking back then, but a much easier approach would be to provide the following additional style and ditch the open event handler:
在 CSS 中,确保宽度设置为
auto
:自动完成小部件创建列表项的位置将 li 空白样式设置为
nowrap
:In the CSS make sure the width is set to
auto
:Where the auto complete widget creates the list items set the li whitespace style to
nowrap
: