输入文本字段文本光标未出现
Input text field text cursor is not appear?
我有两个输入字段,一个是主窗口,另一个是隐藏的模态框。显示了隐藏的(模态框),但是模态框的输入字段光标没有出现,而是出现在后台主窗口的输入字段中。
$(document).ready(function() {
$("#input_2").click(function() {
$(".window").show();
});
$("#close").click(function() {
$(".window").hide();
});
});
#input_1 {
margin: 20px;
padding: 10px;
margin-left: 150px;
}
#input_2 {
margin: 20px;
padding: 10px;
}
.window {
width: 100%;
height: 100%;
position: fixed;
z-index: 10;
background: rgba(0,0,0,.5);
top: 0;
left: 0;
display: none;
}
.popup {
width: 500px;
height: 300px;
background: white;
border-radius: 3px;
position: absolute;
left: 30%;
top: 25%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<div class="window">
<div class="popup">
<h2 style="text-align:center;">Hello World!</h2>
<p style="text-align:center;" id="close">[close]<p>
<!--@first input text field indside popup window-->
<input type="text" id="input_1" />
</div>
</div>
<!--@second input text field outside popup window-->
<input type="text" id="input_2" />
在模态窗口中设置focus()
在input
上
$('#input_2').click(function() {
$('.window').show();
$('#input_1').focus();
});
$(document).ready(function() {
$('#input_2').click(function() {
$('.window').show();
$('#input_1').focus();
});
$('#close').click(function() {
$('.window').hide();
});
});
#input_1 {
margin: 20px;
padding: 10px;
margin-left: 150px;
}
#input_2 {
margin: 20px;
padding: 10px;
}
.window {
width: 100%;
height: 100%;
position: fixed;
z-index: 10;
background: rgba(0, 0, 0, 0.5);
top: 0;
left: 0;
display: none;
}
.popup {
width: 500px;
height: 300px;
background: white;
border-radius: 3px;
position: absolute;
left: 30%;
top: 25%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<div class="window">
<div class="popup">
<h2 style="text-align:center;">Hello World!</h2>
<p style="text-align:center;" id="close">[close]
<p>
<!--@first input text field indside popup window-->
<input type="text" id="input_1" />
</div>
</div>
<!--@second input text field outside popup window-->
<input type="text" id="input_2" />
将autofocus
应用于<input type="text" id="input_1" autofocus>
,您将看到下图。
$(document).ready(function(){
$('#input_2').click(function(){
$('.window').show();
});
$('#close').click(function(){
$('.window').hide();
});
});
#input_1{
margin:20px;
padding:10px;
margin-left:150px;
}
#input_2{
margin:20px;
padding:10px;
}
.window{
width:100%;
height:100%;
position:fixed;
z-index:10;
background: rgba(0,0,0,0.5);
top:0;
left:0;
display:none;
}
.popup{
width:500px;
height:300px;
background:white;
border-radius:3px;
position:absolute;
left:30%;
top:25%;
}
<!DOCTYPE html>
<html>
<head>
<title>PopUp Window</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
</head>
<body>
<div class="window">
<div class="popup">
<h2 style="text-align:center;">Hello World!</h2>
<p style="text-align:center;" id="close">[close]<p>
<!--@first input text field indside popup window-->
<input type="text" id="input_1" autofocus>
</div>
</div>
<!--@second input text field outside popup window-->
<input type="text" id="input_2" />
</body>
</html>
在#input_2
中添加$('#input_1').focus();
点击事件
$(document).ready(function(){
$('#input_2').click(function(){
$('.window').show();
$('#input_1').focus();
});
$('#close').click(function(){
$('.window').hide();
});
});
#input_1{
margin:20px;
padding:10px;
margin-left:150px;
}
#input_2{
margin:20px;
padding:10px;
}
.window{
width:100%;
height:100%;
position:fixed;
z-index:10;
background: rgba(0,0,0,0.5);
top:0;
left:0;
display:none;
}
.popup{
width:500px;
height:300px;
background:white;
border-radius:3px;
position:absolute;
left:30%;
top:25%;
}
<!DOCTYPE html>
<html>
<head>
<title>PopUp Window</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
</head>
<body>
<div class="window">
<div class="popup">
<h2 style="text-align:center;">Hello World!</h2>
<p style="text-align:center;" id="close">[close]<p>
<!--@first input text field indside popup window-->
<input type="text" id="input_1" />
</div>
</div>
<!--@second input text field outside popup window-->
<input type="text" id="input_2" />
</body>
</html>
在弹出输入框时为输入元素添加焦点
.focus ();$(document).ready(function(){
$('#input_2').click(function(){
$('.window').show();
$("#input_1").focus();
});
$('#close').click(function(){
$('.window').hide();
});
});
#input_1{
margin:20px;
padding:10px;
margin-left:150px;
}
#input_2{
margin:20px;
padding:10px;
}
.window{
width:100%;
height:100%;
position:fixed;
z-index:10;
background: rgba(0,0,0,0.5);
top:0;
left:0;
display:none;
}
.popup{
width:500px;
height:300px;
background:white;
border-radius:3px;
position:absolute;
left:30%;
top:25%;
}
<!DOCTYPE html>
<html>
<head>
<title>PopUp Window</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
</head>
<body>
<div class="window">
<div class="popup">
<h2 style="text-align:center;">Hello World!</h2>
<p style="text-align:center;" id="close">[close]<p>
<!--@first input text field indside popup window-->
<input type="text" id="input_1" />
</div>
</div>
<!--@second input text field outside popup window-->
<input type="text" id="input_2" />
</body>
</html>
相关文章:
- 将文本插入光标所在的文本区域
- 无法在Chrome控制台中使用javascript将焦点和光标设置为输入文本
- jQuery/Javascript>on单击将文本放置在最后一个已知的光标位置
- 当用户单击按钮(在光标位置)时,在输入字段中添加一个文本字符串
- 如何在按键事件发生后获取文本光标的位置
- 同时在两个文本框上显示光标
- 我可以通过javascript在鼠标光标旁边动态添加文本吗?
- 将光标放在 javascript 文本框的末尾
- 如何使文本字段中的文本光标以几像素开始
- 在 HTML 中模拟闪烁的文本光标
- 设置文本光标在文本区域中的位置
- 如何获取文本光标在屏幕上的像素位置
- 用自定义字符替换闪烁的文本光标
- 如何覆盖活动的文本光标(Safari)
- 删除要使用Jquery输入的文本光标
- 输入文本字段文本光标未出现
- 当按下向上箭头键时,将文本光标发送到文本框焦点的末尾
- 在HTML文档上显示闪烁文本光标
- 可以知道文本光标在输入字段中的位置
- Canvas onmousedown导致文本光标,无论我使用什么CSS