javascript - jQuery hasClass 'is not a function' -
i want check jquery li element list of li elements active 1 (they represent menu, , want check menu item active).
so, store li items in variable, myelements. ask length of myelements, , apply style changes them, here works. apparently way variable myelements nodelist, not array, declare variable; myelements_array, contains same elements myelements , array (also tested , works).
then try check of elements myelements_array has 'current-menu-item' class, doesn't work , google chrome console says there's error: 'uncaught typeerror: myelements_array[j].hasclass not function'. have idea reason might be?
<script type='text/javascript'> var myelements = jquery("#navbar > ul > li"); var count = myelements.length; (var = 0; < myelements.length; i++) { myelements[i].style.width = (100/count)+'%'; } var myelements_array = []; for(var = myelements.length; i--; myelements_array.unshift(myelements[i])); var j = 0; while (! myelements_array[j].hasclass('current-menu-parent') ) { j++; } document.write(j); </script>
when access jquery object if it's array, returns raw dom element object, not jquery object. if want jquery object, use .eq() rather array index:
while (myelements.eq(j).hasclass('current-menu-parent') ) { j++; } you use:
j = myelements.index(myelements.find(".current-menu-parent:first"));
Comments
Post a Comment