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

Between both those :

Javascript

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

Or

function getCss2(object)
{
    if (object.getAttribute("className")) {
        return object.getAttribute("className");
    }
    return object.getAttribute("class");
}

function setCss2(object, cssclass)
{
    if (object.getAttribute("className")) {
        return object.setAttribute("className",cssclass);
    }
    object.setAttribute("class",cssclass);
}

HTML

<a href="#" onClick="setCss(this, ‘newclass‘)" />
<a href="#" class="something" onClick="alert(getCss(this))" />
<a href="#" onClick="setCss2(this, ‘newclass‘)" />
<a href="#" class="something" onClick="alert(getCss2(this))" />

Both version seems to work in IE8, FF4, Chrome, Opera and Safari. (jsFiddle (improved) demo)

Which one is best-usage practice and why? Did you ever run into any issue with either version?

Answer1:

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.

Answer2:

object.getAttribute("className"); actually does not work.

The difference is that getAttribute gets the value of a HTML attribute as it is written in the HTML code (with some exceptions).

These values are mostly also the initial values of the properties of the DOM element. But accessing them can yield different values, due to pre-/postprocessing.

For example, if you have an <a> element, el.href gives you the complete (absolute) URL, while el.getAttribute(‘href‘) gives you the URL as it was written in the HTML.

Most of the time, you want to access the  properties of the DOM element, as these reflect the current state of the element.

Answer3:

Use the first one.

It‘s sorter. It works in every browser even the very old ones that don‘t support getAttribute. It probably faster too.

But please don‘t use a function for this. Just use this.className and this.className=‘newClass‘. Using a function is overkill for this.

【转载】http://stackoverflow.com/questions/6574946/object-classname-or-object-getattributeclassname-class

时间: 2024-10-20 15:35:15

object.className or object.getAttribute(“className/class”)?的相关文章

Redis存储Object 和 list&lt;object&gt;

Redis 存储支持的类型没有object ,虽然有支持list,但是只支持List<String> 有两种方法可以实现存储对象和泛型 1.用序列化和反序列化 2.json 序列化工具类,实现序列化和反序列话对象和list集合 package com; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.Closeable; import java.io.Object

【JS】☆★之详解[Object HTMLDivElement]和[Object Object]

[JS]☆★之详解[Object HTMLDivElement]和[Object Object] <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml">

遍历对象属性(for in、Object.keys、Object.getOwnProperty)

js中几种遍历对象的方法,包括for in.Object.keys.Object.getOwnProperty,它们在使用场景方面各有不同. for in 主要用于遍历对象的可枚举属性,包括自有属性.继承自原型的属性 var obj = {"name":"Poly", "career":"it"} Object.defineProperty(obj, "age", {value:"forever

Object obj=new Object()的内存引用

Object obj=new Object(); 一句很简单的代码,但是这里却设计Java栈,Java堆,java方法去三个最重要的内存区域之间的关联. 假设这句代码出现在方法体中. 1.Object obj将反映到Java栈的本地变量表,这是一个本地变量的定义.是一个引用类型. 2.new Object()将会反映在Java堆中.存储了Object类型的所有实例数据值(次内存是不固定大小的,因为谁也无法确定这是对象的大小). 3.程序运行,类型信息已经加载到内存里,这些数据就在Java方法区中

[Javascript] Object.freeze() vs Object.seal()

let person = { firstName: "Zhentian", lastName: "Wan" }; /*Object.freeze() makes object cannot be updated, added or deleted*/ let freezePerson = Object.freeze(person); freezePerson.address="Finland"; // Cannot add property ad

Java基础知识强化26:Object类之Object类的概述

1.Object类 类Object是类层次结构的根类,每个类都使用 Object作为超类.所有对象(包括数组)都实现这个类的方法 每个类直接或者间接继承自Object类 2.Object类无参构造: public  Object() 回想面向对象中为什么说:子类的构造方法默认访问的是父类的无参构造. 答:这是因为所有类的共同父亲Object只有一个无参构造. 来自为知笔记(Wiz)

微信小程序把玩(三十一)wx.uploadFile(object), wx.downloadFile(object) API

原文:微信小程序把玩(三十一)wx.uploadFile(object), wx.downloadFile(object) API 反正我是没有测通这两个API!!!!不知道用的方式不对还是其他的!!!先记录下回头再说... 主要方法: wx.uploadFile(OBJECT)上传 wx.downloadFile(OBJECT)下载 wxml <button type="primary" bindtap="listenerButtonDownLoadFile"

jq 插件 的两个相关的函数 jQuery.fn.extend(object); jQuery.extend(object);

jQuery为开发插件提拱了两个方法,分别是:  http://www.cnblogs.com/wyjgreat/archive/2011/07/19/2110754.html JavaScript代码 jQuery.fn.extend(object); jQuery.extend(object); jQuery.extend(object); 为扩展jQuery类本身.为类添加新的方法. jQuery.fn.extend(object);给jQuery对象添加方法. fn 是什么东西呢.查看j

JS Object.getOwnPropertyDescriptor()和Object.defineProperty()

ECMAScript 5 对对象属性进行了重新定义.除了原有的 property:value外,还增加了一些用于 标识该属性是否可写,可枚举,可配置的特性.为此引入了两个新方法:Object.getOwnPropertyDescriptor()和Object.defineProperty(). <script> var person ={ name:"Hai" }; //定义一个新对象 var personName = Object.getOwnPropertyDescri