>  기사  >  웹 프론트엔드  >  js中 aaa.style 和 aaa.getAttribute('style') 等价吗,有无区别?

js中 aaa.style 和 aaa.getAttribute('style') 等价吗,有无区别?

WBOY
WBOY원래의
2016-06-07 08:42:281621검색

js中 aaa.style 和 aaa.getAttribute('style') 等价吗,有没有什么区别?

我自己在各种浏览器(IE6到chrome)测试的结果是一样,没发现什么区别

是完全一样吗?
============================
我知道大概区别了,下面第二句在高级浏览器下面查不到信息
不过我想问的是,为什么在IE6/7下第二句也可以成功执行???
是否说在ie6/7下aaa.style和aaa.getAttribute('style')等价???
alert(aaa.style.height);
alert(aaa.getAttribute('style').height);

回复内容:

路过
这是个老问题
现在应该早就不提了吧

懒的再写
贴点儿老图吧
js中 aaa.style 和 aaa.getAttribute('style') 等价吗,有无区别?
js中 aaa.style 和 aaa.getAttribute('style') 等价吗,有无区别? js中 aaa.style 和 aaa.getAttribute('style') 等价吗,有无区别? js中 aaa.style 和 aaa.getAttribute('style') 等价吗,有无区别? js中 aaa.style 和 aaa.getAttribute('style') 等价吗,有无区别? js中 aaa.style 和 aaa.getAttribute('style') 等价吗,有无区别? js中 aaa.style 和 aaa.getAttribute('style') 等价吗,有无区别? js中 aaa.style 和 aaa.getAttribute('style') 等价吗,有无区别? js中 aaa.style 和 aaa.getAttribute('style') 等价吗,有无区别? js中 aaa.style 和 aaa.getAttribute('style') 等价吗,有无区别? js中 aaa.style 和 aaa.getAttribute('style') 等价吗,有无区别? js中 aaa.style 和 aaa.getAttribute('style') 等价吗,有无区别? js中 aaa.style 和 aaa.getAttribute('style') 等价吗,有无区别? js中 aaa.style 和 aaa.getAttribute('style') 等价吗,有无区别? js中 aaa.style 和 aaa.getAttribute('style') 等价吗,有无区别? js中 aaa.style 和 aaa.getAttribute('style') 等价吗,有无区别? js中 aaa.style 和 aaa.getAttribute('style') 等价吗,有无区别? js中 aaa.style 和 aaa.getAttribute('style') 等价吗,有无区别? js中 aaa.style 和 aaa.getAttribute('style') 等价吗,有无区别?由于写的时间很早,细节上可能有变化
不过大致情况如此
理解下就好了 有区别

首先区分property和attribute,两个翻译成中文都可以作为属性,但是在实际上是有区别的。

在html标签里的属性称为attribute
例如:alaki

这个dom element有3个attribute:href、data-tips、data-original_title

而property是那些它被创建的时候就有的属性,例如attributes, autofocus, className, clientHeight。
特殊的是,假如
alaki
对于这个dom element来说,class不仅是attribute,同时它也是property,但是在dom.element中,只不过它叫className,这两个是绑定的。

简单来说,一些特殊的attribute将会转换为property,脚踏两条船,同样的style也是个脚踏两条船的家伙。

假如是内联样式,通过getAttribute('style')是可以获得的,但只能获取到内联样式部分属性,通过外部样式表或者内嵌样式都是无法获得的,返回值是字符串。

假如不是内联,那么getAttribute('style')返回null或者空字符串,返回哪一个取决于这个浏览器的实现 Element.getAttribute()

上面两种情况,dom.style都将获得完整样式属性,返回值为对象CSSStyleDeclaration

最后一个关于ie6和ie7的问题,
如图
js中 aaa.style 和 aaa.getAttribute('style') 等价吗,有无区别?里面有一句话 :In IE5-7, accessing the style attribute gives an object
DOM Core

在ie5-7里面,getAttribute()的实现是跟dom.style一样的效果的 @alaki 已经说得很好了,我补充下。

elem.style 和 elem.getAttribute('style') 的关系我在这个回答里提到过一点,可以参考一下:webkit内核的浏览器为什么removeAttribute('style')会失效? - 顾轶灵的回答 (里面有些链接好像失效了)

总的来说,style 的内容属性你改成啥就会保留你改后的样子,但是 IDL 属性读时会根据新的内容属性中对应的 CSS 属性来更新,写 IDL 属性时还会重新序列化内容属性以和 IDL 属性保持同步。

举个例子:
HTML:
<code class="language-html"><span class="nt"><div> <span class="na">id=</span><span class="s">"x"</span> <span class="na">style=</span><span class="s">"color: red; aaa: bbb;"</span><span class="nt">></span>
</div></span>
</code>
怎么可能一样,一个是CSSStyleDeclaration对象,一个是字符串。 Are you kidding?
js中 aaa.style 和 aaa.getAttribute('style') 等价吗,有无区别? aaa.style的 style 是dom property, aaa.getAttribute('style') 得到的是html attribute;

html attribute由 html 定义,dom property 由 DOM 定义;
1. 许多 attribute 有与之对应的 property
2. 一些 attribute 没有对应的 property
3. 一些 property 也没有对应的 attribute

比较通用的规则是,html attribute 用于初始化 dom property 的值,之后除非脚本变更,一般不改变,而 dom property则随着用户的交互行为而随之改变,如 input 的 value。

题主的例子不好看出差别,换成 input 的 value 更能看出区别 谢邀。

不等价。碰巧等价也只是因为那是预置固有属性。class(Name)开始就有兼容问题。自定义属性完全行不通。比较可靠的是title这种。style我都怀疑是不是能作为对象用。

实践建议是原生属性一律.xxx,自定义属性一律.getAttribute。后者考虑到兼容性,包括了data-*。

补充:
实测.getAttribute('style')是字符串。
ie7-不支持.setAttribue('style','') IE6/7下 `elem.getAttribute("style")`和`elem.style`返回的都是`CSSStyleDeclaration`对象。
这是个BUG,在IE8之后已经被修复了。
`elem.getAttribute("style")`返回的是元素的style属性上css文本(如果有点话,没有返回null),而`elem.style`返回`CSSStyleDeclaration`。 在Secret Of The JavaScript Ninja 中有详细讲解这两个的区别。 aaa.getAttribute('style')获取的是aaa的内联样式字符串
성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.