最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - javascript event.target not working in mozilla - Stack Overflow

programmeradmin3浏览0评论
<script type ='javascript'>
    function fun(userID) {
        var btn = event.target; // error 'event' undefine in mozilla 
        alert(btn.id);
    }
</script>

<asp:linkButton id ="target" style =" cursor:pointer" onclick ="fun('1')" >click here </asp:LinkButton>

I am new in JavaScript, I have written above code and this code is working fine in Google chrome but not working in Mozilla Firefox. can anyone suggest how to find control firing event?

<script type ='javascript'>
    function fun(userID) {
        var btn = event.target; // error 'event' undefine in mozilla 
        alert(btn.id);
    }
</script>

<asp:linkButton id ="target" style =" cursor:pointer" onclick ="fun('1')" >click here </asp:LinkButton>

I am new in JavaScript, I have written above code and this code is working fine in Google chrome but not working in Mozilla Firefox. can anyone suggest how to find control firing event?

Share Improve this question edited Dec 27, 2012 at 9:04 Cerbrus 72.9k19 gold badges136 silver badges150 bronze badges asked Dec 27, 2012 at 8:58 DurgeshDurgesh 1192 silver badges9 bronze badges 1
  • How is the function called? – Beetroot-Beetroot Commented Dec 27, 2012 at 9:00
Add a ment  | 

5 Answers 5

Reset to default 11

Pass event to the function:

<asp:linkButton id ="target" style =" cursor:pointer" onclick ="fun(event, '1')" >click here </asp:LinkButton>

function fun(event, userID)
{
    event= event|| window.event;
    var btn = event.target; 
    alert(btn.id);
}

OR

Make sure your event is not undefined

function fun(userID)
{
    var e = event || window.event;
    var btn = e.target; 
    alert(btn.id);
}

event is undefined in firefox, because firefox doesn't have this property and instead you can provides an event to an event handler function as a parameter.

However your code is fine to work in Chrome and IE

Further I would also have script a type like <script type ='text/javascript'> and not just <script type ='javascript'>

To access the event in the click handler, attach it in jQuery rather than in the HTML.

HTML:

<a id ="target" style ="cursor:pointer" data-value="1">click here</a>

Javascript:

$(function() {
    $("#target").on('click', function(event) {
        event.preventDefault();
        var btn = this;
        var value = $(this).data('value');
        alert(btn.id + ', ' + value);
    });
});

Many (including myself) consider this the "proper" way to attach event handlers. There are several advantages, of which access to the event object is just one.

If you pass the button using the keyword this then no need for event

...onclick="fun(this,1)"

function fun(but, userID) { 
  var butID=but.id

}

Use arguments[0].target instead of event.target. Most browsers support the event global variable, but firefox does not. arguments[0] contains all the same properties and functions, but is supported by all browsers.

As a side note: indices in the arguments array can be changed by your own code. In my current project arguments[0] contains all of my controller functions for some reason, while the global event methods and properties are contained in arguments[1]. Not entirely sure why this is, but you might need to do some debugging on the arguments array to see what values it contains and adjust accordingly. More often than not however, arguments[0] should work.

发布评论

评论列表(0)

  1. 暂无评论