精简版

element.setAttribute("required", "");    //turns required on

element.required = true;                 //turns required on through reflected attribute

jQuery(element).attr('required', '');    //turns required on

$("#elementId").attr('required', '');    //turns required on

element.removeAttribute("required");     //turns required off

element.required = false;                //turns required off through reflected attribute

jQuery(element).removeAttr('required');  //turns required off

$("#elementId").removeAttr('required');  //turns required off

if (edName.hasAttribute("required")) { }  //check if required

if (edName.required) { }                 //check if required using reflected attribute

长版

一旦TJ Crowder设法指出反射属性,我就会发现以下语法错误:

element.attributes["name"] = value; //bad! Overwrites the HtmlAttribute object

element.attributes.name = value;    //bad! Overwrites the HtmlAttribute object

value = element.attributes.name;    //bad! Returns the HtmlAttribute object, not its value

value = element.attributes["name"]; //bad! Returns the HtmlAttribute object, not its value

你必须经历element.getAttribute和element.setAttribute:

element.getAttribute("foo");         //correct

element.setAttribute("foo", "test"); //correct

这是因为该属性实际上包含一个特殊的HtmlAttribute对象:

element.attributes["foo"];           //returns HtmlAttribute object, not the value of the attribute

element.attributes.foo;              //returns HtmlAttribute object, not the value of the attribute

通过将属性值设置为“true”,您错误地将其设置为String对象,而不是它需要的HtmlAttribute对象:

element.attributes["foo"] = "true";  //error because "true" is not a HtmlAttribute object

element.setAttribute("foo", "true"); //error because "true" is not an HtmlAttribute object

从概念上讲,正确的想法(用打字语言表达)是:

HtmlAttribute attribute = new HtmlAttribute();

attribute.value = "";

element.attributes["required"] = attribute;

这就是为什么:

getAttribute(name)

setAttribute(name, value)

存在。他们完成了将值分配给内部HtmlAttribute对象的工作。

除此之外,还会反映一些属性。这意味着您可以通过Javascript更好地访问它们:

//Set the required attribute

//element.setAttribute("required", "");

element.required = true;

//Check the attribute

//if (element.getAttribute("required")) {...}

if (element.required) {...}

//Remove the required attribute

//element.removeAttribute("required");

element.required = false;

什么,你不想要做的就是误用.attributes集合:

element.attributes.required = true;     //WRONG!

if (element.attributes.required) {...}  //WRONG!

element.attributes.required = false;    //WRONG!

测试案例

这导致围绕使用required属性进行测试,比较通过属性返回的值和反射属性

document.getElementById("name").required;

document.getElementById("name").getAttribute("required");

结果:

HTML                         .required        .getAttribute("required")

==========================   ===============  =========================

false (Boolean)  null (Object)

true  (Boolean)  "" (String)

true  (Boolean)  "" (String)

true  (Boolean)  "required" (String)

true  (Boolean)  "true" (String)

true  (Boolean)  "false" (String)

true  (Boolean)  "0" (String)

试图.attributes直接访问该集合是错误的。它返回表示DOM属性的对象:

edName.attributes["required"] => [object Attr]

edName.attributes.required    => [object Attr]

这就解释了为什么你不应该.attributes直接与收藏品交谈。您不是在操纵属性的值,而是操纵属性本身的对象。

如何设置要求?

设置required属性的正确方法是什么?您有两个选择,可以是反射属性,也可以通过正确设置属性:

element.setAttribute("required", "");         //Correct

edName.required = true;                       //Correct

严格地说,任何其他值都将“设置”该属性。但Boolean属性的定义规定只应将其设置为空字符串""以指示true。下面的方法都工作到设定的required 布尔属性,

但不要使用它们:

element.setAttribute("required", "required"); //valid, but not preferred

element.setAttribute("required", "foo");      //works, but silly

element.setAttribute("required", "true");     //Works, but don't do it, because:

element.setAttribute("required", "false");    //also sets required boolean to true

element.setAttribute("required", false);      //also sets required boolean to true

element.setAttribute("required", 0);          //also sets required boolean to true

我们已经了解到尝试直接设置属性是错误的:

edName.attributes["required"] = true;       //wrong

edName.attributes["required"] = "";         //wrong

edName.attributes["required"] = "required"; //wrong

edName.attributes.required = true;          //wrong

edName.attributes.required = "";            //wrong

edName.attributes.required = "required";    //wrong

如何清除所需?

努力当诀窍删除的required属性是很容易不小心打开它:

edName.removeAttribute("required");     //Correct

edName.required = false;                //Correct

使用无效方式:

edName.setAttribute("required", null);    //WRONG! Actually turns required on!

edName.setAttribute("required", "");      //WRONG! Actually turns required on!

edName.setAttribute("required", "false"); //WRONG! Actually turns required on!

edName.setAttribute("required", false);   //WRONG! Actually turns required on!

edName.setAttribute("required", 0);       //WRONG! Actually turns required on!

使用反射.required属性时,您还可以使用任何“falsey”值将其关闭,并使用truthy值将其打开。但为了清晰起见,坚持真假。

如何检查的required?

通过以下.hasAttribute("required")方法检查属性是否存在:

if (edName.hasAttribute("required"))

{

}

您还可以通过布尔反射.required属性检查它 :

if (edName.required)

{

}

html5对属性布尔的值设定,如何在Javascript中设置HTML5必需属性?相关推荐

  1. html字体怎么设置大写,如何在html中设置字体的属性

    如何在html中设置字体的属性 发布时间:2021-06-08 17:45:33 来源:亿速云 阅读:72 作者:Leah 这篇文章给大家介绍如何在html中设置字体的属性,内容非常详细,感兴趣的小伙 ...

  2. html 获取文本框值,html - 如何在JavaScript中获取文本框值

    html - 如何在JavaScript中获取文本框值 我正在尝试使用JavaScript从HTML文本框中获取值,但值不是在空格之后 例如: 我只得到:上面的"软件". 我正在使 ...

  3. visio调整形状位置_如何在Visio 中设置形状的属性?

    Visio 2010允许用户通过具体的设置项目,更改形状的属性. 1.更改尺寸和位置 用户除了可以通过拖动形状手柄调整形状的属性外,还可以通过精确的数值定义形状的尺寸和其他多种属性.选择形状后,用户即 ...

  4. JavaScript 中遍历对象的属性

    原文链接 JavaScript 中遍历对象的属性 参考 JavaScript中的属性:如何遍历属性 <JavaScript 高级程序设计> 概述 遍历 JavaScript 对象中的属性没 ...

  5. JavaScript 中遍历对象的属性 1

    JavaScript 中遍历对象的属性 原文链接 JavaScript 中遍历对象的属性 参考 JavaScript中的属性:如何遍历属性 <JavaScript 高级程序设计> 概述 遍 ...

  6. JavaScript中函数的length属性

    JavaScript中函数的length属性 length是函数的一个属性 函数的length是js函数对象的一个属性,函数的length代表形参的个数(即有多少必传参数) 形参的数量不包括不包括剩余 ...

  7. JavaScript中的可枚举属性与不可枚举属性

    在JavaScript中,对象的属性分为可枚举和不可枚举之分,它们是由属性的enumerable值决定的.可枚举性决定了这个属性能否被for-in查找遍历到. 一.怎么判断属性是否可枚举 js中基本包 ...

  8. 如何在Javascript中访问对象的第一个属性?

    本文翻译自:How to access the first property of an object in Javascript? Is there an elegant way to access ...

  9. Hibernate继承(2)子类属性生成在自己的一张表中,父类的属性一张表

    Hibernate继承(2)子类属性生成在自己的一张表中,父类的属性一张表 <?xml version="1.0"?> <!DOCTYPE hibernate-m ...

最新文章

  1. 【c语言】蓝桥杯算法训练 大小写转换
  2. 浅析网站SEO优化用户体验值包括什么后续又该如何进行提升?
  3. Java移除ssl认证_Java SSL证书吊销检查
  4. 设计之路:如何进行软件需求分析?
  5. 数据源 连接oracle
  6. 计算机一级电子表格插入表格,计算机一级电子表格
  7. 热点科普 | 自监督视觉特征学习
  8. 全套安全从业人员必备工具(建议大家收藏这个帖子)
  9. 指针变量使用规则实例
  10. java email 正则 验证
  11. 32位java jre_jre-7u4-windows-i586.exe|java 1.7.0(Java TM 7)JRE7 32位_最火软件站
  12. 分享一款手机最强Python编程神器,用手机运行Python。天秀!
  13. Java实现支付宝网页支付
  14. (转)移动端自适应方案
  15. mysql long varchar2_long类型字段转换成varchar2类型
  16. 力科(Lecroy)示波器专用波形文件(轨迹文件/trace文件/.trc文件)在MATLAB上的解析与回写
  17. pairing-friendly曲线家族
  18. 小米电视 android版本升级,小米电视2系统版本多久更新一次
  19. From MAP, MLE, OLS, G-N to IEKF,EKF
  20. 知识库 wcp php,WCP

热门文章

  1. 动态加载jar包(一)
  2. 【论文写作】课程指导平台的开发中系统部分代码如何写
  3. matlab如何判断一个文件夹里面是否包含某个含有部分文件名的文件_如何构建一个成功的AI PoC(概念验证项目)...
  4. java 不可修改对象_Java并发编程(六)不可改变对象
  5. android多板面式布局,Android Design
  6. 成都计算机学校分数线,成都市计算机汽车职业技术学校2020年招生录取分数线...
  7. 步进电机的加速时间是怎么样的?
  8. word2vec -- 负采样 -- skip-gram
  9. Failed to create AppDomain 'xxx'. Exception has been Failed to create AppDomain
  10. 初窥Javascript单元测试,附带掌握一门新技能的学习方式。