点击IE / Firefox中的事件触发,但Chrome正在删除事件分配 [英] Click event fires in IE/Firefox, but Chrome is dropping the event assignment

查看:193
本文介绍了点击IE / Firefox中的事件触发,但Chrome正在删除事件分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在调试我的网络应用程序,并打了墙。我在Google Chrome中只遇到一种行为,而我的javascript无能为力使我无法解决问题。



我有一个ASP页面,一个 ; asp:Panel> 控件。在面板中,我设置了一个简单的搜索文本框,并使用< asp:LinkBut​​ton> 来启动搜索。用户输入搜索文本,并且应该能够输入(对于可用性的原因 ),并显示搜索结果。这在IE中工作,但在FireFox中不起作用。有一个记录的修复我已经应用到我的页面,并已经成功地使FireFox运行。金色。



除了此修复程序在Google Chrome中不起作用!有点腥味,我开火Firebug来调试代码...哦,等等...这是一个仅Chrome的问题。好的,我可以处理调试javascript没有Firebug( sob ) - 我启动了Chrome调试器,并逐步完成代码。事实证明,前面提到的JavaScript修复程序正在被Chrome删除。



修复脚本在页面加载时运行,并修改$ $ c $的点击处理程序c> LinkBut​​ton :

  var defaultButton = document.getElementById('<%= lnkSearch.ClientID %>'); 
if(defaultButton&&&"(defaultButton.click)=='undefined'){
defaultButton.click = function(){
alert('function fired');
var result = true;
if(defaultButton.click)result = defaultButton.onclick();
if(typeof(result)=='undefined'|| result){
eval(defaultButton.getAttribute('href'));
}
};
alert(typeof(defaultButton.click)!='undefined');
}

当在chrome调试器中运行页面时,我进入 function WebForm_FireDefaultButton()并得到以下行:

  if(defaultButton&& typeof(defaultButton.click)!=undefined){...} 

由于某种原因 defaultButton.click 已成为undefined。我被骗了...我失踪了什么?



此外,我没有使用jQuery,它不是一个可行的解决方案。 >



生成的HTML:

 < div id =abc_pnlSearchPanel语言=javascriptonkeypress =javascript:return WebForm_FireDefaultButton(event,'abc_lnkSearch')> 
< div class =searchPanel>
< span class =searchText>
输入要搜索的内容:
< / span>
< span style =float:left;>
< input name =abc:txtSearchtype =textid =abc_txtSearchstyle =width:312px; />
< / span>
< a onclick =this.blur(); id =abc_lnkSearchclass =ButtonLayouthref =javascript:__ doPostBack('abc $ lnkSearch','')>< span>搜索< / span>< / a>
< div style =clear:both>< / div>
< / div>
< / div>


解决方案

我无法确定为什么会发生这种情况在Chrome中,没有其他浏览器。但是注册在PageLoad上执行的脚本解决了这个问题。

 < body onLoad =DefaultButtonFix()> 


I'm in the process of debugging my web application and have hit a wall. I'm experiencing a behavior in Google Chrome only and my javascript ineptitude is keeping me from the solution.

I have an ASP page with an <asp:Panel> control. Within the panel, I've setup a simple search textbox and am using an <asp:LinkButton> to launch the search. The user enters their search text and should be able to hit enter (for usability sake) and the search results will display. This works in IE, but not in FireFox. There is a documented fix which I've applied to my page and have successfully gotten FireFox to function. Golden.

Except, the fix doesn't work in Google Chrome! A bit fishy, I fire up Firebug to debug the code... oh wait... it's a Chrome only issue. OK, fine I can handle debugging javascript without Firebug (sob) - I fire up the Chrome debugger and step through the code. It turns out that the javascript fix, previously mentioned, is being dropped by Chrome.

The fix script runs on page load and modifies the click handler of the LinkButton:

var defaultButton = document.getElementById('<%= lnkSearch.ClientID %>');
if (defaultButton && typeof(defaultButton.click) == 'undefined') {   
    defaultButton.click = function() {
        alert('function fired');
        var result = true;
        if (defaultButton.click) result = defaultButton.onclick();
        if (typeof(result) == 'undefined' || result) {
            eval(defaultButton.getAttribute('href'));
        }
    };
    alert(typeof(defaultButton.click) != 'undefined');
}

And when running the page in the chrome debugger I step into function WebForm_FireDefaultButton() and get to the line:

if (defaultButton && typeof(defaultButton.click) != "undefined") { ... }

and for some reason defaultButton.click has become "undefined". I'm stumped... What am I missing?

Also, I'm not using jQuery and it isn't a viable solution.


The HTML produced:

<div id="abc_pnlSearchPanel" language="javascript" onkeypress="javascript:return WebForm_FireDefaultButton(event, 'abc_lnkSearch')"> 
    <div class="searchPanel"> 
        <span class="searchText"> 
            Type in stuff to search:
        </span> 
        <span style="float: left;">
            <input name="abc:txtSearch" type="text" id="abc_txtSearch" style="width:312px;" />
        </span> 
        <a onclick="this.blur();" id="abc_lnkSearch" class="ButtonLayout" href="javascript:__doPostBack('abc$lnkSearch','')"><span>Search</span></a> 
        <div style="clear: both"></div> 
    </div> 
</div> 

解决方案

I wasn't able to determine why this was happening in Chrome and none of the other browsers. But registering the script to execute on PageLoad solved the problem.

<body onLoad="DefaultButtonFix()">

这篇关于点击IE / Firefox中的事件触发,但Chrome正在删除事件分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆