如何防止光标从回车?

时间:2016-08-09 16:46:04

标签: javascript html css forms

我有一个简单的搜索字段div,但问题出在这里:

enter image description here

我想使用我的代码(而不是另一个库)阻止这种方式的回车,

这是我的html:

<div id="search" contenteditable="true" oninput="searchChanged()"></div>

使用Javascript:

function searchChanged () {
    queries.search = document.getElementById("search").innerText;
    if (!queries.search.match(/\S/)) { delete queries.search; }
    updateURL();
    updateSearch();
}

和CSS:

#search {
    width:200px;
    height:25px;
    background-color:#fcfcfc;
    outline: 1px solid #eee;
    focus:none;
    position:absolute;
    text-align: left;
    vertical-align: middle;
    right:5px;
    margin-top:22px;
    padding-left:5px;
}

1 个答案:

答案 0 :(得分:0)

解决方案

function searchChanged () {
    queries.search = document.getElementById("search").innerText;
    if (!queries.search.match(/\S/)) { delete queries.search; }
    updateURL();
    updateSearch();
}
#search {
    width:200px;
    height:60px;
    background-color:#fcfcfc;
    outline: 1px solid #eee;
    focus:none;
    position:absolute;
    text-align: left;
    vertical-align: middle;
    overflow:auto;
    margin-top:22px;
    padding-left:5px;
}
#search br{
display:none;
}
<div id="search" contenteditable="true" oninput="searchChanged()"></div>