The function of jQuery.fn
not and assign anything, it just pass the selected nodes to your custom method. Who defines whether will add repeatedly and Voce.
In case Voce can use the attr()
to detect if Node has already received the attribute (if Voce is only going to use for nodes), I recommend that you also check if the selected item is really a Node, because the $(...)
can receive window and Document also.
function isDOM(el) {
return el && el instanceof HTMLElement;
}
jQuery.fn.foo = function() {
this.each(function() {
if (isDOM(this) && $(this).attr("data-foo") !== "true") {
$(this).attr("data-foo", "true");
//Algo aqui
}
});
};
This way Voce can make several different selectors and it will detect if the html element has already received the expected effect.
If what Voce wants is to remove the effect on elements that already have it, Voce can do so:
function aplicaEfeito(el) {
//Aqui aplica o efeito
}
function removeEfeito(el) {
//Aqui remove o efeito
}
function isDOM(el) {
return el && el instanceof HTMLElement;
}
jQuery.fn.foo = function() {
this.each(function() {
if (isDOM(this)) {
if ($(this).attr("data-foo") !== "true") {
$(this).attr("data-foo", "true");
aplicaEfeito($(this));//Aplica o efeito
} else {
removeEfeito($(this));//Primeiro remove o efeito
aplicaEfeito($(this));//Aplica o efeito novamente
}
}
});
};
it is somewhat complicated to answer this question, without knowing what has in this function something, because thinking a function that manipulates the
dom
of the element, with the "reset
" would you know what was changed? it would be necessary to create a variable with if it was a change log.– Guilherme Lautert
Could Orion give more details? For the
.fn.
has the function of just going through the selected elements with a custom function, the problem seems to be in your Function, however Voce can maybe use an attribute in the elements to detect if they were already set. I’ll try to formulate a response.– Guilherme Nascimento