Thursday 15 March 2012

jquery - javascript-being-dumb experience of the day -


It seems that changing the abbreviation of 3 characters to represent a number will be an easy task. Javascript says, "NOPE!"

  $ (selector) .each (function () {// short name numerical representation var monthStr = $ (this) .text (). / [^ \ /] * /) [0]; Var months = {Jan: 1, FEB: 2, March: 3, APR: 4, May: 5, June: 6, July: 7, August: 8, SEP: 9, OCT: 10, NOV: 11, DEC: 12}; Var month = months [months] .ststring (); Var date = $ (this) .text (). Replace (month month, month); $ (This) .text (date);});   

Although the code is valid and works, my console still messes with the following error:

Unkit type error: method 'toString 'Can not call

There is no point in this because the text of the selector is correct Has changed (according to the least eyes).

Month STR 'variable if I have set it as an example in a static' MAR 'and removes' .toString ()' from 'my' variable definition, then error is no longer is.

In addition, the '.toString ()' has been mentioned before, if the conversion for my month variable was not so, then I would end up with an 'undefined' value.

Just wanted to share your javascript-dumb experience and hoped why someone could explain this error was thrown, even when the code works at no time.

The problem should be most of:

$ (selector) .each (function () {...});

What is the selector, and how many dome objects are being selected? It seems that a DOM object is being selected, which does not have text.

Here is an example that repeats your error: (View Console)

Console: 'Uncount Type Error: Undefined' toString 'Can not Call Method'

  & lt; P & gt; March / 26/2013 and lt; / P & gt; & Lt; P & gt; Hello World! & Lt; / P & gt; $ ('P') each (function () {var month = $ (this) .text (). Match (/ [^ \ /] * /) [0]; var months = {MAR: 3}; alert ( Month [months] .toString ());});   

The reason for this is that we will not allow two & lt; P & gt; elements are selected, and there is only one date as one.

If we limit our code to DOM objects as text, then it works fine:

  & lt; P & gt; MAR / 26/2013 & lt; / P & gt; Var month = $ ('P'). Text (). Match (/ [^ \ /] * /) [0]; Var months = {MAR: 3}; Warning (months [month] .toString ());   

You should use a CSS class for those elements which are text in the form of a date, and for these elements, you know what you are choosing like this .

No comments:

Post a Comment