Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 为什么我不能附加到焦点事件,但可以附加到文本框的模糊事件?_Jquery_Focus - Fatal编程技术网

Jquery 为什么我不能附加到焦点事件,但可以附加到文本框的模糊事件?

Jquery 为什么我不能附加到焦点事件,但可以附加到文本框的模糊事件?,jquery,focus,Jquery,Focus,简单代码: jQuery(document).ready(function () { var txtAtm = jQuery("#txtAtm"); txtAtm.on("focus", function () { alert("Focus!"); }); //txtAtm.blur(function () { // alert("Blur!"); //}); txtAtm.on("blur", function () { alert("Blur!"

简单代码:

jQuery(document).ready(function () {
    var txtAtm = jQuery("#txtAtm");
    txtAtm.on("focus", function () {
    alert("Focus!");
});

//txtAtm.blur(function () {
//    alert("Blur!");
//});

  txtAtm.on("blur", function () {
    alert("Blur!");
  });

});
模糊代码工作正常,聚焦代码不工作!!有什么想法吗

如果我将事件更改为focusin,它会工作,但捕获Tab键也会移动框中的内容

该表单是一个带有以下标记的webform,我想用jQuery替换内联标记(可以!!),只是想确认我可以捕获事件!!(第一关失败!!):

未设置
呈现的HTML类似于webform控件的标准autopost代码:

<input name="ctl00$leftContent$ctl07$txtAtm" type="text" value="not set"
  onchange="javascript:setTimeout('WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(&quot;ctl00$leftContent$ctl07$txtAtm&quot;, &quot;&quot;, true, &quot;prv&quot;, &quot;&quot;, false, true))', 0)" 
  onkeypress="if (WebForm_TextBoxKeyHandler(event) == false) return false;" 
  id="txtAtm" class="withUnit">

onkeypress您正在使用的
return false
导致您的代码无法工作


如果您删除它,它将按您希望的那样开始工作。

谢谢您的快速回答,看起来这是一个时间问题!!看起来“ready”不是“ready!”,以下代码有效:

jQuery(document).ready(function () {
///<summary>
/// Replaces the inline markup
///</summary>

var txtAtm = jQuery("#txtAtm");

setTimeout(function () {
    txtAtm.on("focus", function () {
        txtAtm.select();
        //alert("Focus!");
    })
}, 1000);


txtAtm.on("blur", function () {
    //alert("Blur!");
    initBox(this.id, 'not set');
});

txtAtm.on("click", function () {
    txtAtm.select();
});
jQuery(文档).ready(函数(){
///
///替换内联标记
///
var txtAtm=jQuery(“#txtAtm”);
setTimeout(函数(){
txtAtm.on(“焦点”,函数(){
txtAtm.select();
//警惕(“聚焦!”);
})
}, 1000);
txtAtm.on(“模糊”,函数(){
//警报(“模糊!”);
initBox(this.id,“未设置”);
});
txtAtm.on(“单击”,函数(){
txtAtm.select();
});
}))


令人恼火的是,脚本ref也在页面的末尾,因此您可能会期望在计时方面出现最小的问题

它们都不应该工作,因为您需要在jQuery中使用clientId-例如
$(“#”)
@Pete:显示的呈现id显然已经与代码正确:
id=“txtam”
啊,是的,对不起,我将名称作为id来读取-通常,在asp控件中,id也是这样呈现的,因为代码是由asp注入的,是的,这个想法也在我脑海中闪过——MVC吸引我的另一个原因!
jQuery(document).ready(function () {
///<summary>
/// Replaces the inline markup
///</summary>

var txtAtm = jQuery("#txtAtm");

setTimeout(function () {
    txtAtm.on("focus", function () {
        txtAtm.select();
        //alert("Focus!");
    })
}, 1000);


txtAtm.on("blur", function () {
    //alert("Blur!");
    initBox(this.id, 'not set');
});

txtAtm.on("click", function () {
    txtAtm.select();
});