我收到此消息 TypeError:document.addEventListener(...)不是函数
(document.addEventListener('polymer-ready',function($){
console.log('ready')
// top link
$.each(decodeURI(location.pathname).split('/'), function() {
$("#self").html(this+'');
});
// browser default lang
default_lang = navigator.language /* Mozilla */ || navigator.userLanguage /* IE */;
// does URL contain lang=xx ?
$.each(decodeURI(location.search.substr(1)).split('&'), function() {
var s = this.split('=');
if (s[0]=="lang") default_lang = s[1];
});
// toplevel = earth
$("#earth").click(
function(e) {
e.preventDefault();
//location.href = "http://www.geonames.org";
}
);
// entry point
geo_click($("#earth"));
}))(jQuery);
有人可以告诉我这有什么问题吗?
答案 0 :(得分:0)
您的代码无效您将document.addEventListener视为IIFE,而不是。您正在错误的位置执行(jQuery)。
答案 1 :(得分:0)
您将jQuery传递给立即函数而不是事件侦听器。
(function ($) {
document.addEventListener('polymer-ready', function () {
console.log('ready')
// top link
$.each(decodeURI(location.pathname).split('/'), function () {
$("#self").html(this + '');
});
// browser default lang
var default_lang = navigator.language /* Mozilla */ || navigator.userLanguage /* IE */;
// does URL contain lang=xx ?
$.each(decodeURI(location.search.substr(1)).split('&'), function () {
var s = this.split('=');
if (s[0] == "lang") { default_lang = s[1] };
});
// toplevel = earth
$("#earth").click(
function (e) {
e.preventDefault();
//location.href = "http://www.geonames.org";
}
);
// entry point
geo_click($("#earth"));
});
})(jQuery);
答案 2 :(得分:0)
解决了问题!
function geoready(){
console.log('ready');
// top link
$.each(decodeURI(location.pathname).split('/'), function() {
$("#self").html(this+'');
});
// browser default lang
default_lang = navigator.language /* Mozilla */ || navigator.userLanguage /* IE */;
// does URL contain lang=xx ?
$.each(decodeURI(location.search.substr(1)).split('&'), function() {
var s = this.split('=');
if (s[0]=="lang") default_lang = s[1];
});
// toplevel = earth
$("#earth").click(
function(e) {
e.preventDefault();
//location.href = "http://www.geonames.org";
}
);
// entry point
geo_click($("#earth"));
}
我在Polymer构造函数中绑定它:
<script>
Polymer({
is: "geo-dropdown",
ready: geoready
});
</script>