Comment supprimer un attribut d’un élément HTML avec jQuery

Dans ce tutoriel vous allez découvrir comment supprimer des attributs d’un élément HTML à l’aide de jQuery.
 
 
Dans l’exemple, lorsque vous cliquez sur le bouton « Supprimer le lien », l’attribut href sera supprimé du lien à l’aide de la méthode removeAttr() de jQuery.
 

Code source :
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Supprimer un attribut d'un élément HTML avec jQuery</title>
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$(".sup-attr").click(function(){
$("a").removeAttr("href");
});
});
</script>
</head>
<body>
<button type="button" class="sup-attr">Supprimer le lien</button>
<a href="https://waytolearnx.com">www.waytolearnx.com</a>
</body>
</html>
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Supprimer un attribut d'un élément HTML avec jQuery</title> <script src="https://code.jquery.com/jquery-1.12.4.min.js"></script> <script type="text/javascript"> $(document).ready(function() { $(".sup-attr").click(function(){ $("a").removeAttr("href"); }); }); </script> </head> <body> <button type="button" class="sup-attr">Supprimer le lien</button> <a href="https://waytolearnx.com">www.waytolearnx.com</a> </body> </html>
<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>Supprimer un attribut d'un élément HTML avec jQuery</title>
		<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
		<script type="text/javascript">
			$(document).ready(function() {
				$(".sup-attr").click(function(){            
					$("a").removeAttr("href");
				});
			});
		</script> 
	</head>
	<body>
		<button type="button" class="sup-attr">Supprimer le lien</button>
		<a href="https://waytolearnx.com">www.waytolearnx.com</a>
	</body>
</html>
Résultat
www.waytolearnx.com
QCM jQuery

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *