通过属性或setAttribute设置属性 [英] Setting a property via property or setAttribute

查看:111
本文介绍了通过属性或setAttribute设置属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

其中一个比其他更好?为什么?性能如何 - 如果这些被称为数千次?

  A)element.setAttribute(disabled,true); 
B)element.disabled = true;

他们都似乎禁用了FF 4中的输入[text]元素。

解决方案

一般来说...



使用属性。很长一段时间(直到版本7或8 IIRC)Internet Explorer已经严重破坏了 setAttribute 的实现,该属性将不属于该属性(经典的故障点为 class ,因为没有属性(它是 className



在这种情况下,特别是... element.setAttribute(disabled,true); 是错误的应该是 element.setAttribute(disabled,disabled);


Is one of these more preferable than the other? Why? How about performance--if these are being called thousands of times?

A) element.setAttribute("disabled", true);
B) element.disabled = true;

They both seem to disable an input[text] element in FF 4.

解决方案

In general…

Use properties. For a long time (until version 7 or 8 IIRC) Internet Explorer had a seriously broken implementation of setAttribute that would set the property not the attribute (the classic point of failure was class since there is no class property (it is className).

In this case in particular… element.setAttribute("disabled", true); is wrong. It should be element.setAttribute("disabled", "disabled");

这篇关于通过属性或setAttribute设置属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆