问题
I'm getting this message TypeError: document.addEventListener(...) is not a function
(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);
Can someone tell me what is wrong with that?
回答1:
Your code is not valid you are treating the document.addEventListener as an IIFE which it is not. You are executing the (jQuery) at the wrong location.
回答2:
You pass jQuery into the immediate function not into the event listener.
(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);
回答3:
Problem Solved!
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"));
}
And I bind that in the Polymer constructor:
<script>
Polymer({
is: "geo-dropdown",
ready: geoready
});
</script>
来源:https://stackoverflow.com/questions/31432874/how-to-make-onchange-event-in-polymer