我想要一个漂亮的小图标,当点击它时,它会清除<输入>盒

这是为了节省空间,而不是在输入框之外有一个清晰的链接.

我的CSS技能很弱...这是一张iPhone外观的截图.

推荐答案

从HTML5开始,您可以使用<input type="search">.但这不一定是可定制的.如果您想要完全控制UI,这里有两个启动示例.一个使用jQuery,另一个不使用.

使用jQuery:

<!DOCTYPE html>
<html lang="en">
    <head>
        <title>SO question 2803532</title>
        <script src="https://code.jquery.com/jquery-latest.min.js"></script>
        <script>
            $(document).ready(function() {
                $('input.deletable').wrap('<span class="deleteicon"></span>').after($('<span>x</span>').click(function() {
                    $(this).prev('input').val('').trigger('change').focus();
                }));
            });
        </script>
        <style>
            span.deleteicon {
                position: relative;
                display: inline-flex;
                align-items: center;
            }
            span.deleteicon span {
                position: absolute;
                display: block;
                right: 3px;
                width: 15px;
                height: 15px;
                border-radius: 50%;
                color: #fff;
                background-color: #ccc;
                font: 13px monospace;
                text-align: center;
                line-height: 1em;
                cursor: pointer;
            }
            span.deleteicon input {
                padding-right: 18px;
                box-sizing: border-box;
            }
        </style>
    </head>
    <body>
        <input type="text" class="deletable">
    </body>
</html>

没有jQuery

jQuery不是严格需要的,它很好地将渐进式增强所需的逻辑与源代码分开,当然您也可以使用plain HTML/CSS/JS:

<!DOCTYPE html>
<html lang="en">
    <head>
        <title>SO question 2803532, with "plain" HTML/CSS/JS</title>
        <style>
            span.deleteicon {
                position: relative;
                display: inline-flex;
                align-items: center;
            }
            span.deleteicon span {
                position: absolute;
                display: block;
                right: 3px;
                width: 15px;
                height: 15px;
                border-radius: 50%;
                color: #fff;
                background-color: #ccc;
                font: 13px monospace;
                text-align: center;
                line-height: 1em;
                cursor: pointer;
            }
            span.deleteicon input {
                padding-right: 18px;
                box-sizing: border-box;
            }
        </style>
    </head>
    <body>
        <span class="deleteicon">
            <input type="text">
            <span onclick="var input = this.previousElementSibling; input.value = ''; input.focus();">x</span>
        </span>
    </body>
</html>

最终只会得到更难看的HTML(和非跨浏览器兼容的JS;).

同样,如果你最关心的不是用户界面的外观,而是功能,那么就用<input type="search">代替<input type="text">.它将在支持HTML5的浏览器上显示(特定于浏览器的)清除按钮.

Html相关问答推荐

CSS Flex行之间的空间很大

Rmarkdown上的垂直标签集

在Apps Script中连接CSS与HTML

试图让三个Divs与下面的另外三个对齐

如何更改计时器S输入的Angular 字体大小?

仅向上扩展并以最大高度滚动的Div

如何用css在右侧创建多个半圆

为什么带有截断的 contentedible div 在受约束时会在 Chrome 中添加空格

Div 容器不会遵守边距、填充或间隙

jquery向单词中的一组字母添加跨度

可以通过悬停时的自定义区域更改文本属性吗?

SVG 填充 colored颜色 不使用轮廓填充空白区域

如何将 HTML DateTime 转换为 Golang Time 对象

在 vscode 中找不到 htmltagwrap 命令

将背景图像裁剪成两半

如何为某些行具有 rowspan 的表的每个奇数行着色

把标题、段落和图片放在一起

如何将第一个 p 标签放在其下方第二个 p 标签的中间

列宽等于最宽列宽度的无界容器

禁用的 Select 标签没有在 chrome 上的 css 中指定的 colored颜色