如何添加“搜索”文本输入字段中的按钮?
如何创建与 这个网站?
如果我们查看源代码,他有一个文本框,后面跟着一个 标签。
<input type="text" name="q" id="site-search-input" autocomplete="off" value="Search" class="gray" />
<span id="g-search-button"></span>
在哪里可以获得类似的“放大镜”图像?
How do I create a similar “search” element to the one in this site?
If we view source, he has one textbox followed by a <span>
tag.
<input type="text" name="q" id="site-search-input" autocomplete="off" value="Search" class="gray" />
<span id="g-search-button"></span>
Where do I get a similar "magnifying glass" image?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
将图像放入范围中,例如使用
background-image
,然后给它一个相对位置并将其向左移动,使其与搜索框的右端重叠,例如:JSBin 上的工作示例
Put the image into the span, for example using
background-image
, then give it a relative position and move it to the left so it overlaps the right end of the search box, for example:Working example on JSBin
你的眼睛在欺骗你。该按钮不在文本框中。使用背景图像不是的方法,因为它不会提供可点击的提交按钮。
您需要做的是在 input:text 和 input:submit 周围添加一个包装器 div。
包装器看起来就像一个文本框,但实际上包含一个透明文本框和一个提交按钮。您需要专门删除 input:text 和 input:submit 元素的样式。
保留提交按钮非常重要,否则在搜索时按 Enter 键将不会有默认反应。此外,将提交按钮放置在文本字段后面可以让人们实际单击该按钮。
您可以制作自己的放大图像,用 20x20px 透明 png 制作它们非常容易。
Your eyes are deceiving you. The button is not within the text box. Using a background image is NOT the way to go, as it wont provide the clickable submit button.
What you need to do is add a wrapper div around the input:text and input:submit.
The wrapper will look like it's a text box, but will actually contain a transparent text box and a submit button. You'll need to specifically remove the styles for the input:text and input:submit elements.
It's very important that you keep the submit button, otherwise hitting enter while searching will not have a default reaction. Additionally placing the submit button after the text field allows people to actually click on the button.
You can make your own magnifying image, they're pretty easy to make in a 20x20px transparent png.
如果您在 Google Chrome 中查看该页面,右键单击搜索按钮并选择“检查元素”,您将能够看到用于实现此效果的 CSS。
如果您不熟悉 CSS,我强烈推荐'CSS:权威指南'。
If you view the page in Google Chrome, right-click on the search button and select “Inspect element”, you’ll be able to see the CSS used to achieve this effect.
If you’re not familiar with CSS, I thoroughly recommend ‘CSS: The Definitive Guide’.
像 Iconspedia 这样的网站有许多类似的免费图标。
无论您在何处获得该图标,请务必确保您有权在应用程序中使用它。许多图形受到保护,有些图形具有限制性许可证。
A site like Iconspedia has a number of free icons that are similar.
Wherever you get the icon be careful to ensure that you have the rights to use it in your application. Many graphics are protected and some have restrictive licenses.
如果您在字段上使用背景图像,则无法绑定到它来获取单击操作。所以解决方案是有一个单独的搜索字段和图像,这样你就可以将 jQuery 中的单击事件绑定到图像。在这里小提琴:
http://jsfiddle.net/Lzm1k4r8/23/
您可以调整
left :
位置位于搜索框的左侧或右侧。If you use a background image on the field then there's no way to bind to it to get the click action. So the solution is to have a separate search field and image, so you can bind click event in jQuery to the image. Fiddle here:
http://jsfiddle.net/Lzm1k4r8/23/
You can adjust
left:
position to be left or right side of the search box.为了帮助获得用户反馈,为什么不在将鼠标悬停在放大镜上时向鼠标添加指针图标呢?只需将此添加到您的 CSS:
.search:hover {
光标:指针;
}
To help with user feedback why not add the pointer icon to your mouse when you're hovering over the magnifying glass? Just att this to your CSS:
.search:hover {
cursor:pointer;
}
我想插入一个我编写的新 jQuery 插件,因为我觉得它满足了 OP 的请求。
它称为 jQuery.iconfield:https://github.com/yotamofek/jquery.iconfield。
它可以让您轻松地将图标添加到文本字段的左侧。为了将图标用作按钮,您可以轻松绑定到“iconfield.click”事件,该事件在用户单击图标时触发。当鼠标悬停在图标上时,它还负责更改光标。
例如:
我很想得到一些关于我的工作的反馈。
I'd like to plug a new jQuery plugin I wrote because I feel it answers to the OP's request.
It's called jQuery.iconfield: https://github.com/yotamofek/jquery.iconfield.
It lets you easily add an icon to the left side of your text field. For using the icon as a button, you can easily bind to the 'iconfield.click' event, which is triggered when the user clicks the icon. It also takes care of changing the cursor when the mouse is hovering over the icon.
For instance:
I would love to get some feedback on my work.