如何使用jQuery在文本字段中设置光标位置?我有一个包含内容的文本字段,我希望用户光标在关注字段时定位在某个偏移处.代码应该看起来像这样:
$('#input').focus(function() {
$(this).setCursorPosition(4);
});
Run Code Online (Sandbox Code Playgroud)
setCursorPosition函数的实现是什么样的?如果您有一个内容为abcdefg的文本字段,则此调用将导致光标定位如下:abcd**|**efg.
Java有一个类似的功能,setCaretPosition.javascript是否存在类似的方法?
更新:我修改了CMS的代码以使用jQuery,如下所示:
new function($) {
$.fn.setCursorPosition = function(pos) {
if (this.setSelectionRange) {
this.setSelectionRange(pos, pos);
} else if (this.createTextRange) {
var range = this.createTextRange();
range.collapse(true);
if(pos < 0) {
pos = $(this).val().length + pos;
}
range.moveEnd('character', pos);
range.moveStart('character', pos);
range.select();
}
}
}(jQuery);
Run Code Online (Sandbox Code Playgroud) 我有一个输入文字:
<input name="Email" type="text" id="Email" value="email@abc.com" />
Run Code Online (Sandbox Code Playgroud)
我想提出一个默认值,比如"你的编程问题是什么?具体." 在StackOverFlow中,当用户点击它时,默认值为disapear.
可能重复:
将光标设置为文本框的14个焦点
我能够在Firefox和IE中做到这一点.但由于某种原因,它不适用于Chrome和Safari :(
我只是使用下面的onfocus
$('input:text').focus(
function(){
document.getElementById('id').setSelectionRange(0, 0);
});
Run Code Online (Sandbox Code Playgroud)
有人可以告诉我如何在Chrome和Safari中做类似的事情吗?
有谁知道如何在我的搜索栏中制作这样的效果:https: //www.dropbox.com/help
<input type="text" name="input">
我的意思是onFocus和onBlur效果,文字消失并动态显示.
谢谢大家!
这是不是等同于这个问题,但它是非常相似的.
我正在使用ASP.Net并且需要或多或少地做同样的事情,但希望它可以在ASP方面完成.
我的相关代码如下:
<td style="line-height: 230%;">
<asp:TextBox ID="txtePro" runat="server" CssClass="textbox" Font-Size="Small" Height="18px" Width="100px" Visible="False" Wrap="False" OnTextChanged="txtEPro_OnLeave" AutoPostBack="true"></asp:TextBox>
<asp:MaskedEditExtender ID="MeeePro" runat="server" Mask="9999999" MaskType="None" TargetControlID="txtePro" PromptCharacter="_" />
<asp:MaskedEditExtender ID="MeePRD" runat="server" Mask="999999" MaskType="None" TargetControlID="txtePro" PromptCharacter="_" />
<asp:RegularExpressionValidator Display="Dynamic" ControlToValidate="txtePro" ID="RevePro" ValidationExpression="^[\s\S]{7,7}$" runat="server" ErrorMessage="7 Digits required." ForeColor="White" Font-Size="X-Small"></asp:RegularExpressionValidator>
<asp:RegularExpressionValidator Display="Dynamic" ControlToValidate="txtePro" ID="RevPRD" ValidationExpression="^[\s\S]{6,}$" runat="server" ErrorMessage="6 Digits required." ForeColor="White" Font-Size="X-Small"></asp:RegularExpressionValidator>
<asp:TextBox ID="hdntxtePro" runat="server" CssClass="textbox" Font-Size="Small" Height="18px" Width="100px" Visible="False" Wrap="False"></asp:TextBox>
<asp:DropDownList ID="ddlCIT" runat="server" AutoPostBack="True" CssClass="textbox" Visible="false" Height="20px" OnSelectedIndexChanged="ddlCIT_SelectedIndexChanged" Width="100px"></asp:DropDownList>
<asp:TextBox ID="txtCIT" runat="server" …Run Code Online (Sandbox Code Playgroud)