JQuery 的attr() 与css() 的区别
1.attr是用来获得或设置标签属性的(attribute的缩写)
var myId = $("#myId");
myId.attr("data-name", "baidu");
// 结果为 : <div id="myId" data-name="baidu"></div>
// 设置属性名data-name,值baidu
var attr = myId.attr("data-name"); // 获取
相对于
var myId = document.getElementById("myId");
myId.setAttribute("data-name", "baidu");
myId.getAttribute("data-name");
2.css是设置元素的style样式的
var myId = $("#myId");
myId.css("background-color", "red");
var bg = myId.css("background-color");
相对于
var myId = document.getElementById("myId");
myId.style.backgroundColor = "red";
var bg = myId.style.backgroundColor;