简体   繁体   English

JavaScript单击“事件”传递给单击位置的函数

[英]Javascript click “event” passed to a function for click location

Somewhere in the HTML there is an input. HTML中的某处有输入。 When the input is clicked the event should be passed to a function to get the clicked location. 单击输入后,应将事件传递给函数以获取单击的位置。

<input type=".." onclick="namespace.aFunction("param", event)" />

(function (namespace, $, undefined) {
    namespace.aFunction = function(string, event) {
        var clickX;
        var clickY;

        // IE Only
        var e = window.event || e;
        clickX = e.clientX;
        clickY = e.clientY;
        //... alert to debug or do something
    };
} (window.namespace = window.namespace || {}, jQuery));

Passing "this" didn't help in IE. 传递“ this”在IE中没有帮助。 Is there a good way to get the click event to get the location of the clicked item that works cross browser etc.? 是否有一种很好的方法来获取click事件,以获取在跨浏览器等环境下工作的被单击项的位置?

This began working in IE after deleting the cache so this seems to work. 删除缓存后,它开始在IE中工作,因此似乎可以正常工作。 The event parameter had been null after I added it but it was just due to the change not refreshing in the browser. 添加后,event参数为null,但这只是由于更改未在浏览器中刷新而已。

<input type=".." onclick="namespace.aFunction("param", event)" />

(function (namespace, $, undefined) {
    namespace.aFunction = function(string, event) {
        var clickX;
        var clickY;

        if (event != 'undefined' && event != null && event.clientX != null && event.clientY != null) {
            clickX = event.clientX;
            clickY = event.clientY;
            //... alert to debug or do something
        }
    };
} (window.namespace = window.namespace || {}, jQuery));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM