1. 程式人生 > >data-id 和 id 的區別

data-id 和 id 的區別

作者:Zeropoint零點
來源:CSDN
原文:https://blog.csdn.net/qq_41648132/article/details/80364335
版權宣告:本文為Zeropoint零點原創文章,轉載請附上博文連結!

 

id是選擇器data-id只是行記憶體放資料的一個標籤,跟input裡面的value作用是一樣的同時在HTML5 中增加了一項新功能是 自定義資料屬性 ,也就是 data-* 自定義屬性。在HTML5中我們可以使用以 data- 為字首來設定我們需要的自定義屬性,來進行一些資料的存放。


<style type="text/css">  div[data-id="something"]{  background-color: yellow; } </style> <body>  <div data-id="something">hello world</div>  <button>click me</button> </body> <script type="text/javascript" src="js/jquery.js"></script> <script type="text/javascript">  $(document).ready(function(){   $("button").on("click",function(){    $("div").removeAttr("data-id");    //滑鼠單擊時移除div的背景色   })  }) </script>