object.className or object.getAttribute(“className/class”)?

前端 未结 3 666
后悔当初
后悔当初 2021-01-12 13:00

Between both those :

Javascript

function setCss(object, css) {
    return (object.className = css);
}
function getCss(object, css) {
    return ob         


        
3条回答
  •  执念已碎
    2021-01-12 13:54

    getAttribute("class") is more universal, because it can be used in different types of documents. In XML documents, most importantly. Including SVG.

    element.className works only in HTML. It is described in the DOM level 2 HTML specs.

提交回复
热议问题