OUJOOD.COM
Définition et Usage La méthode removeAttr()
jQuery cours tutorialLa méthode .removeAttr() supprime un attribut spécifié des éléments sélectionnés.
La méthode .removeAttr() utilise la fonction removeAttribute() de JavaScript, mais elle a l'avantage de pouvoir être appelée directement sur un objet jQuery et elle tient compte de différent noms de l'attribut à travers les navigateurs.
Note : La suppression d'un gestionnaire d'événements onclick à l'aide de .removeAttr() ne donne pas l'effet désiré dans Internet Explorer. Pour éviter ce problème potentiel, utilisez plutôt la méthode .prop() :
Syntaxe
$(sélecteur).removeAttr(attribut)
Paramètre |
Description |
|---|---|
|
attribut |
Obligatoire. Spécifie l'attribut pour supprimer |
Exemple : Supprimez l'attribut de style de tous les éléments p
Exemple : 📝 Copier le code
<!DOCTYPE html>
<html>
<head>
<script
src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("p").removeAttr("style");
});
});
</script>
</head>
<body>
<h1>Ceci est un titre</h1>
<p style="font-size:120%;color:red">Ceci est un
paragraphe.</p>
<p>Ceci est un autre paragraphe.</p>
<button>Suprimer l'attribut style </button>
</body>
</html>
Autre exemple :
Exemple : 📝 Copier le code
<!DOCTYPE html>
<html>
<head>
<script
src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
<button>Suprimer l'attribut style </button>
<input type="text" title="Salut tout le monde" />
<div id="log"></div>
<script>
(function() {
var inputTitle = $("input").attr("title");
$("button").click(function () {
var input =
$(this).next();
if ( input.attr("title") ==
inputTitle ) {
input.removeAttr("title")
} else {
input.attr("title", inputTitle);
}
$("#log").html( "L'attribut
title est maintenant: " + input.attr("title") );
});
})();
</script>
</body>
</html>
</body>
</html>
Par gerywa 25 juillet 2014