简体   繁体   English

从JS模块内部的HTML访问函数

[英]Accessing a Function from HTML inside of a JS Module

So I can currently enqueing the script as so. 因此,我目前可以将脚本排入队列。

wp_enqueue_script( 'myScripts', WPEX_JS_DIR_URI .'/myScripts.js', array( 'jquery' ), '1.7.5', true );

And I am currently attempting to use the function as seen below. 我目前正在尝试使用如下所示的功能。

(function($) {


     function vote_up () {
        $(".voteButton").html("Hello jQuery");
     }


})(jQuery);

And I am just for the sake of getting it work attempting to call it with an onclick method, 我只是为了让它能够尝试使用onclick方法调用它而工作,

<div class="button voteButton" onclick="vote_up()">Vote Up !</div>

I understand that I may need to assign the function like this, 我了解我可能需要分配这样的功能,

var foo = (function ($) {

Although when I do this and attempt to call it with 虽然当我这样做并尝试用

foo.vote_up()

I still cannot get it to work. 我仍然无法正常工作。

Am I missing something simple? 我是否缺少简单的东西?

Thank you. 谢谢。

If you want to invoke the method as object method ( foo.vote_up() ) then return an object from you immediate function instead of only create a function, and then invoke the method. 如果要将该方法作为对象方法( foo.vote_up() )调用,则从您的直接函数中返回一个对象,而不仅仅是创建一个函数,然后调用该方法。 I think that you're looking for something like: 我认为您正在寻找类似的东西:

var foo = (function($) {
     return {
      vote_up : function () {
        $(".voteButton").html("Hello jQuery");
      }
     }
})(jQuery);

foo.vote_up();

Hope this helps, 希望这可以帮助,

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

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