jquery removeAttrメソッドを使って指定した属性を削除する

環境
Google Chrome 105.0.5195.127
Windows 10 Home 64bit
jquery 3.6.1
構文
$(“指定要素").removeAttr(“属性名")
「removeAttr」を使用して指定した属性を削除します。

使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$('#btndel').on('click', function() {
$('#city').removeAttr("value")
})
});
</script>
</head>
<body>
<input id="city" type="text" value="東京">
<p><button id="btndel">削除</button></p>
</body>
</html>
<!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script> <script> $(document).ready(function(){ $('#btndel').on('click', function() { $('#city').removeAttr("value") }) }); </script> </head> <body> <input id="city" type="text" value="東京"> <p><button id="btndel">削除</button></p> </body> </html>
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
  
  $('#btndel').on('click', function() {      

    $('#city').removeAttr("value")
    
  })
});
</script>
</head>
<body>
 <input id="city" type="text" value="東京">          
 <p><button id="btndel">削除</button></p>
</body>
</html>

実行結果
「削除」ボタンを押すと、、指定した属性「value」を削除します。

jQuery

Posted by arkgame