如何在输入文本中隐藏闪烁的光标?

use*_*707 26 html javascript css jquery

我想做一些看起来像选择输入的东西但实际上不是,这里是步骤.

我做了一个<input type="text">.

我添加了一个background-image,它会显示一个"选择箭头",给人的印象是它是一个选择框.

我在此输入中添加了一个默认值.

SlideDown()当我点击它时,会有一个隐藏的div 在这个输入下面.

我尝试了只读的东西,以便无法更改值,但闪烁的光标将显示出来.

如果我使用disabled,闪烁的光标将不会显示,但jQuery中的.click().focus函数将无法正常工作.下拉菜单不会SlideDown().

如何在不显示闪烁光标的情况下使其可点击?

这是代码

<div style="padding-top:17px; overflow:hidden;">
    <div style="float:left;">
        <label for="secretquestion">Secret Question</label><br>
        <input type="text" class="inputselect" id="secretquestion" name="secretquestion" value="Choose a Secret Question" tabindex=10 /><br>
        <div class="selectoptions" id="secretquestionoptions">
            <b>test</b>
        </div> 
    </div>
</div>
Run Code Online (Sandbox Code Playgroud)

CSS

.selectoptions
{
    display: none;
    background-color: white;  
    color: rgb(46,97,158); 
    width: 250px; 
    margin:auto; 
    border-style: solid; 
    border-width:1px; 
    border-color: rgb(46,97,158);  
    font-size: 14px; 
    text-align: center; 
}

.inputselect {
    color: white;  
    cursor: pointer;  
    background-image: url('inputselect.png');
    padding-top: 5px; 
    padding-bottom: 5px;   
    padding-left:10px; 
    padding-right:-10px;
    width:240px; 
    border-style: solid; 
    border-color: rgb(46,97,158); 
    border-width: 1px;
} 
.inputselect:hover {
    outline:none;      
    color:aqua; 
    cursor: pointer; 
    background-image: url('inputselecthover.png');
}
.inputselect:focus {
    outline:none;      
    color:aqua; 
    cursor: pointer; 
    background-image: url('inputselecthover.png');
}
Run Code Online (Sandbox Code Playgroud)

小智 57

只需将此添加到“inputselect”类:

caret-color: transparent;
Run Code Online (Sandbox Code Playgroud)


Orw*_*ile 42

光标的颜色与文本的颜色相匹配.

<input type="text" style="color: transparent">
Run Code Online (Sandbox Code Playgroud)

如果你真的想在输入框中显示文本(我的,有些人是有需要的),你可以使用文本阴影.

<style>
    body, div, input {
       color: #afa;
       text-shadow: 0px 0px 1px #fff;
    }
<style>
Run Code Online (Sandbox Code Playgroud)

(测试过Firefox和Safari).

  • 哇!真是太棒了!也可以写`text-shadow:0 0 0 #fff;` - 在原始的相同位置给出相同的清晰文本. (8认同)
  • 似乎不适用于IE 10 http://jsfiddle.net/plowdawg/mk77W/仍然显示黑色闪烁光标. (5认同)
  • 对不起,IE 现在怎么办?我用的是 Mac。 (2认同)
  • 这个想法很酷,但不幸的是,它在iOS8上也不起作用 (2认同)

rtc*_*ner 9

我只是用模糊来摆脱光标.

$('input').mousedown(function(e){
  e.preventDefault();
  $(this).blur();
  return false;
});
Run Code Online (Sandbox Code Playgroud)


小智 6

我认为这是一个完美的解决方案:使输入足够宽,向右对齐屏幕,从而使光标和内容位于屏幕外部,同时仍可点击

完美解决方案