Javascript removal method?

In this code snippet from AdvancED DOM Scripting:

Call delete(classes[i]); Is this an array or object method? I can not answer Google.

 /** * remove a class from an element */ function removeClassName(element, className) { if(!(element = $(element))) return false; var classes = getClassNames(element); var length = classes.length //loop through the array in reverse, deleting matching items // You loop in reverse as you're deleting items from // the array which will shorten it. for (var i = length-1; i >= 0; i--) { if (classes[i] === className) { delete(classes[i]); } } element.className = classes.join(' '); return (length == classes.length ? false : true); }; window['ADS']['removeClassName'] = removeClassName; 
0
source share
3 answers

The Mozilla help docs state the following regarding the delete statement:

The delete operator deletes an object, an object property, or an element at a specified index in an array.

For more information, see the following article:

http://perfectionkills.com/understanding-delete/

+2
source

delete set the value of the specified element (variable / array / object) to undefined

array / object example ...

since classes[i] actually refers to the index i array. It will set this index position to undefined, reserving the position in the array ...

+1
source

I think you can just use $('p').removeClass('myClass yourClass') with jquery and connect the function for this for any element

0
source

Source: https://habr.com/ru/post/920067/


All Articles