2016-08-21 19 views
0

現在、私は完全にIE、クロム、サファリのために、私はGridviewの中に焦点を当てているテキストボックスのID名を取得するための機能があります。代わりにevent.srcElement.id

function onTextFocus() { 
     alert(event.srcElement.id); 
    } 

機能は、GridViewのためのRowDataBound中に呼び出されます。

protected void ItemGridView_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    if (e.Row.RowType == DataControlRowType.DataRow) 
    { 
     ((TextBox)e.Row.FindControl("txtQuantity")).Attributes.Add("onfocus", "onTextFocus();");    
    } 
} 

それはsrcElementを認識しないとして、それは、Firefoxのために動作しませんが。だから私はすべてのブラウザで動作する代替を探しています。 Googleを精練した後、私はこれらの選択肢を考え出しましたが、私は未定義のエラーまたはReferenceErrorを取得します。なぜどんなアイデア?

function onTextFocus() { 
     alert(this.id); 
     alert(event.currentTarget.id); 
     alert(event.target.id);  
     alert(event.currentTarget); 
     alert(event.target);       
     alert($(this).attr('id')); 
     alert($(obj).attr("id"));    
     alert($(this).id); 
     alert($(this).get(0).id);    
     alert($(this).prop("id")); 
    } 
+0

ショーどのように、あなたが方法 – Satpal

+0

を使用しているが問題は、することができます解決されます。また、いくつかのより多くの支援が必要ですか? –

答えて

0

更新を開始します 今、私はDOTNETのコードを見ていること:あなたがeventパラメータを追加した場合、それはそれに

を渡す必要があり

protected void ItemGridView_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    if (e.Row.RowType == DataControlRowType.DataRow) 
    { 
     ((TextBox)e.Row.FindControl("txtQuantity")).Attributes.Add("onfocus", "onTextFocus(event);");    
    } 
} 

しかし、依存on whatあなたは本当に良い方法があるかもしれないと思っています。

エンド更新

短い答えはここevent.targetを使用している、それはそれは、主要なブラウザでサポートされて示してMDN https://developer.mozilla.org/en-US/docs/Web/API/Event/target上の仕様です。あなたのケースでは、イベント処理関数にパラメータeventを追加する必要があります。

function onTextFocus(event) { 
    alert(event.target.id); 
} 

eventパラメータを宣言していないとアクセスできません。 (それはまた、あなたがイベントを結合されている方法によって異なります)、ここで標準のJavaScript(https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListenerをチェックアウト)、短い例で

:jQueryを使って

window.onload = function(){ 
 
    
 
    document.getElementById("test").addEventListener('focus', onclick1); 
 

 
    function onclick1(event){ 
 
     console.info(event.target.id); 
 
    } 
 
};
<input type="text" id="test" />

(チェックアウトこのリンクhttp://api.jquery.com/on/)ここに短い例:

$(function(){ 
 
    
 
    $("#test1").on('focus', onclick1); 
 

 
    function onclick1(event){ 
 
     console.info(event.target.id); 
 
    } 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<input type="text" id="test1" />