简体   繁体   English

如何在Javascript中向onclick添加名称空间功能?

[英]How can I add a namespace function to onclick in Javascript?

After discovering about Javascript namespaces, I tried to implement them but I run into a problem while trying to attach a namespace method to an element's onclick. 在发现Javascript名称空间之后,我尝试实现它们,但是在尝试将名称空间方法附加到元素的onclick时遇到问题。

I used this method to wrap up my functions/methods/classes (a simplified concept, not my actual code): 我用这种方法包装了我的函数/方法/类(一个简化的概念,而不是我的实际代码):

;(function(window, undefined) {

    //my namespace
    var NS = {};

    NS.test = {
        f : function(param) {
            alert(param);
        }
    }

    NS.test.('test 2');

})(window);

Inside, everything works fine and "test 2" is prompted. 内部,一切正常,并提示“ test 2”。

However, when I try to attach that function to a click event, by doing something like this: 但是,当我尝试通过以下操作将该功能附加到click事件时:

<a href-"#" onclick="NS.test.f('test');">Click me!</a>

it doesn't work, just like it doesn't work when I call that function after the })(window); 它不起作用,就像我在})(window);之后调用该函数时不起作用一样})(window); part. 部分。

I tried it calling it window.NS.test.f('test'); 我尝试将其称为window.NS.test.f('test'); but with no effect. 但没有效果。

How can I make an onclick event call my function? 如何使onclick事件调用我的函数?

I could attach an event listener inside my wrapper, like I do for other html elements with no difficulty, but it would be problematic in this case since I'm generating the links with javascript and I find it easier and simpler to just add onclick="doSomething" for all my links, instead of creating them, then cache them and add event listeners. 我可以在包装器内附加一个事件侦听器,就像我毫不费力地处理其他html元素一样,但是在这种情况下这是有问题的,因为我正在使用javascript生成链接,而我发现添加onclick="doSomething"会更容易为我所有的链接onclick="doSomething" ,而不是创建它们,然后将其缓存并添加事件侦听器。

Call me lazy, but in this particular case I prefer to do 叫我懒,但在这种情况下,我更喜欢做

someDiv.innerHTML = my_Generated_Html_Code_With_OnClick;

instead of 代替

//demo code, ignore the flaws and the fact it won't work on IE
someDiv.innerHTML = my_generated_Html_code;
myLink = document.getElementById(id);
myLink.addEventListener('mousedown', NS.test.f('test'));

I do not use any framework nor do I wish to, since I'm trying to get a better understanding of the so-called vanilla javascript first. 我不使用任何框架,也不希望这样做,因为我想首先更好地理解所谓的javascript。

I set up a jsfiddle here . 在这里设置了一个jsfiddle。

PS I must admit I didn't understand namespaces completely so if I'm doing something wrong here or applying the concept in a way I am not supposed to, I would appreciate any tips or corrections PS:我必须承认我不完全理解名称空间,因此,如果我在此处做错了事或以某种我不应该使用的方式应用了概念,我将不胜感激任何提示或更正

That's because NS is declared inside and hence only exists inside the function: 这是因为NS在内部声明,因此仅存在于函数内部:

function(window, undefined) {
    var NS = {};

    // NS exists here ...
}

// ... but not here

If you want to make it available to the rest of the page, then you can do: 如果要使其在页面的其余部分可用,则可以执行以下操作:

function(window, undefined) {

    var NS = window.NS = {};

    // NS and window.NS exist here ...
}

// ... and window.NS exists here.

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

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