javascript - Prototype addMethods is returning "undefined" -
I am trying to add a method to the element object of the prototype called attecen (), which is the DOM node And my tag on the tagname will return to:
element .addMethods ({locateAncestor: function (element, tag) {element.ancestors (.) Each (function (e) {if (Tag == e. Tag) {warning (e.id); return e;}}})}}});
Then I call the code like this:
var form = target.locateAncestor ('FORM'); Warning (form);
This definitely triggers two warning () boxes first, which is said inside each () loop, and successfully alerts the ID of the element (in this case , A FORM element). This is the first warning (below), I "e" (which when alert is applied to "HTMLFormElement"), as expected. However, when this function is called, the second warning () Undefined ", no value is expected to return to me, which is definitely" HTMLFormElement ".
Is there any important step that is unavailable to add a method, give me some value What should be done for?
There is a return statement for each of your functions, but there is no return statement for the locateAncestor function. Besides this you should use "find out" instead of "each", because you only have to An item from the oreate is required.
element .addMethods ({locateAncestor: function (element, tag) {return element.ancestors (). Detect (function (e) {if (tag == e.tagName) {Warning (e.id); Return;} Return False;});}});
Comments
Post a Comment