The jQuery.fn
function does not assign anything, it just passes the selected nodes to your custom method. Who defines if it will add repeatedly and you.
In case you can use attr()
to detect if the node has already received the attribute (if you will only use it for nodes), I recommend that you also check that the selected item is really a node, since $(...)
can receive window and document as well.
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
}
});
};
In this way you can make several different selectors and it will detect if the html element already has the expected effect.
If what you want and remove the effect on elements that already have it, you 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
}
}
});
};