【问题标题】:Cross browser editable span跨浏览器可编辑范围
【发布时间】:2013-10-19 14:21:16
【问题描述】:

Here 是一个可编辑 span 元素的简单示例。

可以通过点击 lt 来编辑 Span 元素 - 出现输入。

<span>Hello</span>
<input type="text" style="display:none;" value=""/>
<style>
    span, input {    
        font-family: arial, sans-serif;
        font-size: 14px;
    }
    span {
        cursor: pointer;
    }
</style>
<script>
    $(function() {
        $("span").click(function() {
            $(this).hide();
            $("input").val($(this).text()).show();
        });
        $("input").keypress(function(e) {
            if (e.which == 13) {
                $(this).hide();
                $("span").text($(this).val()).show();
            }
        });
    });
</script>

我希望输入中的文本与 span 中的文本处于精确位置。

所以我要添加边距:

对于 Chrome:

input {
    margin-left: -2px;
    margin-top: -2px;
}

对于 IE 10 和 Opera:

input {
    margin-left: -3px;
    margin-top: -2px;
}

对于火狐:

input {
    margin-left: -4px;
    margin-top: -2px;
}

我可以制作一个通用的 css,它可以在任何浏览器中运行而无需任何特殊技巧吗?

【问题讨论】:

    标签: html css cross-browser margin html-input


    【解决方案1】:

    contenteditable 属性应该适合您。它可以工作all the way back to ie5.5

    &lt;span contenteditable&gt;&lt;/span&gt;

    http://jsfiddle.net/CCJMe/

    【讨论】:

    • 感谢您的回复!当跨度宽度结束时,我如何才能在不换行的情况下像输入一样工作?
    • 应该可以将white-space: nowrap;添加到span中。
    • 无论如何它都会破坏跨度宽度。
    • 谢谢。我想用the only input element 来做,但你的解决方案似乎更容易。
    • 这是迄今为止最好的解决方案!原生支持。会为 Angular 使用不同的东西来绑定到控制器变量。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-25
    • 2010-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多