How to make onchange event in Polymer

旧巷老猫 提交于 2019-12-12 01:27:34

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!