0

見婁代碼 這裏是HTML如何設置字符的第一位置,在文本框的文本框點擊事件--- Chrome的問題

<asp:TextBox ID="txtInTime" runat="server" CssClass="TextBox" Width="60px" 
    OnClick="fnFocus(this.id)" ></asp:TextBox> 

這裏的JavaScript

function fnFocus(id) { 
    document.getElementById(id).focus();  
    } 

上面的代碼工作的所有瀏覽器期望鉻... 我想要文本框點擊第一個位置的文本框的焦點..在鉻沒有在第一個位置的字符.. 請幫助... 任何建議...?

回答

1

可以使用setSelectionRange做到這一點:

function fnFocus(id) { 
    var input = document.getElementById(id); 
    input.focus(); 
    input.setSelectionRange(0, 0); 
} 

演示:Fiddle

+0

它在Chrome中工作....但它在IE中的影響。在IE瀏覽器的第一次點擊工作,但如果你點擊第二次不工作 – Kalyan

+0

@Kalyan:我已經更新了代碼和小提琴演示。 –

+0

謝謝@Palash ...在IE中的相同問題..但它的工作在一定程度上.. – Kalyan

0

setSelecionRange在Chrome中爲我工作。

試試這個X-瀏覽器:

//函數來設置光標位置,然後我們設置位置爲您的文本框

$.fn.setCursorPosition = function(pos) {this.each(function(index, elem) { 
if (elem.setSelectionRange) { 
    elem.setSelectionRange(pos, pos); 
} else if (elem.createTextRange) { 
    var range = elem.createTextRange(); 
    range.collapse(true); 
    range.moveEnd('character', pos); 
    range.moveStart('character', pos); 
    range.select(); 
} }); return this; }; 

$('#txtInTime').setCursorPosition(0); 

//在你的代碼

<asp:TextBox ID="txtInTime"...... OnClick="$(this).setCursorPosition(0);"></asp:TextBox> 

Source

+0

它不工作....謝謝你試試.. – Kalyan

+0

當然? 「$('#txtInTime')。setCursorPosition(0);」應該是onclick屬性,如: – seba47

+0

對不起...繼續這裏....如果你在字段的ONCLICK中啓動函數,它會是這樣的: seba47

相關問題