[英]Google Closure onclick event not working
I have the following button in a HTML file: 我在HTML文件中有以下按钮:
<button id="signupbutton" name="signup.submit" type="submit" class="btn">Sign up</button>
I am using pyramid and I include closure base and my script like this: 我正在使用金字塔,并且包括了闭包库和我的脚本,如下所示:
<script src="${request.static_url('app:javascripts/closure-library/closure/goog/base.js')}"></script>
<script>
goog.require('goog.events');
goog.require('goog.events.EventType');
goog.require('goog.dom')
goog.require('goog.dom.forms');
</script>
<script src="${request.static_url('app:javascripts/checks.js')}"></script>
In my javascript file checks.js I wrote: 在我的javascript文件checks.js中,我写道:
var buton = goog.dom.getElement('signupbutton');
goog.events.listen(
buton,
goog.events.EventType.CLICK,
function(e) {
alert("Here");
});
What I get in the console is a TypeError: src is null events.js:954 (var listenerMap = src[goog.events.LISTENER_MAP_PROP_];)
and nothing happens, only the page reloads! 我在控制台中得到的是
TypeError: src is null events.js:954 (var listenerMap = src[goog.events.LISTENER_MAP_PROP_];)
,但没有任何反应,仅页面重新加载!
If I create the button using google closure it works! 如果我使用Google闭包创建按钮,那么它会起作用! So I can't figure out what the problem is!
所以我不知道是什么问题!
its seem because your button is not in the DOM
when your javascript is executed. 似乎是因为执行JavaScript时,您的
button is not in the DOM
。
You should execute your script only after the button's been loaded in the DOM. 仅在将按钮加载到DOM中之后,才应执行脚本。
TypeError: src is null events.js:954
TypeError:src为null events.js:954
Example : http://jsfiddle.net/zeroc00l/ZugNX/ 范例: http : //jsfiddle.net/zeroc00l/ZugNX/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.