简体   繁体   中英

J-Query Error when loading the page

I keep getting an error in my jquery file: "Uncaught TypeError: Cannot call method 'click' of null (anonymous function) jQuery.extend.ready DOMContentLoaded - line 2

Jquery code:

  $(document).ready(function () {
      $(".leftImage a").click(function () {
          _gaq.push(['_trackEvent', 'sidebanner', 'click', $(this).attr('id'), 0, true]);
      }); //End Of SideBanners

      $("a[href$='.pdf'], area[href$='.pdf']").click(function () {
          if ($(this).attr("id")) {
              _gaq.push(['_trackEvent', 'pdf', 'click', $(this).attr('id'), 0, true]);
          } else {
              _gaq.push(['_trackEvent', 'pdf', 'click', $(this).attr('href'), 0, true]);
          }
      }); //End of pdfs mapping and non mapping

      $("a[href$='.zip']").click(function () {
          if ($(this).attr("id")) {
              _gaq.push(['_trackEvent', 'zipfile', 'download', $(this).attr('id'), 0, true]);
          } else {
              _gaq.push(['_trackEvent', 'zipfile', 'download', $(this).attr('href'), 0, true]);
          }
      }); //End of Zips

  });

Appears to be a conflict and you can solve by using the provided solution from http://api.jquery.com/jquery.noconflict/

jQuery.noConflict();

(function( $ ) {

  $(document).ready(function() {

        $(".leftImage a").click(function () {
            _gaq.push(['_trackEvent', 'sidebanner', 'click', $(this).attr('id'), 0, true]);
        }); //End Of SideBanners

        $("a[href$='.pdf'], area[href$='.pdf']").click(function () {
            if ($(this).attr("id")) {
                _gaq.push(['_trackEvent', 'pdf', 'click', $(this).attr('id'), 0, true]);
            } else {
                _gaq.push(['_trackEvent', 'pdf', 'click', $(this).attr('href'), 0, true]);
            }
        }); //End of pdfs mapping and non mapping

        $("a[href$='.zip']").click(function () {
            if ($(this).attr("id")) {
                _gaq.push(['_trackEvent', 'zipfile', 'download', $(this).attr('id'), 0, true]);
            } else {
                _gaq.push(['_trackEvent', 'zipfile', 'download', $(this).attr('href'), 0, true]);
            }
        }); //End of Zips

  });

})(jQuery);

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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