setAttribute和Java中的点表示法有什么区别? [英] What is the difference between setAttribute and dot notation in Javascript?

查看:52
本文介绍了setAttribute和Java中的点表示法有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
何时在JavaScript中使用setAttribute vs .attribute =?

为什么有时会设置这样的属性:

Why do you sometimes set an attribute like this:

x.type = "submit"; 

以及其他类似的时间:

x.setAttribute("type", "submit");

我一直认为用哪种方式都没关系,但是这样做有问题:

I always figured it didn't matter which way, but I'm having an issue doing this:

x.onClick = save;

但是当我切换到它时,它会起作用:

but when I switch it to this it works:

x.setAttribute("onClick", "save()");

推荐答案

setAttribute 仅适用于DOM元素,而对HTML元素使用小写的属性名称.而且,您不能使用点符号为动态属性名称分配值.

setAttribute only works on DOM elements and lowercases the attribute name on HTML elements. And you can't use dot notation to assign values to dynamic attribute names.

还有这个:

使用 setAttribute()修改某些属性,尤其是值在XUL中,工作不一致,因为该属性指定了默认值价值.要访问或修改当前值,您应该使用特性.例如,使用 elt.value 代替 elt.setAttribute('value',val).

Using setAttribute() to modify certain attributes, most notably value in XUL, works inconsistently, as the attribute specifies the default value. To access or modify the current values, you should use the properties. For example, use elt.value instead of elt.setAttribute('value', val).

这篇关于setAttribute和Java中的点表示法有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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