如何生成具有无价值属性的DOM节点 [英] How to generate DOM nodes with valueless attributes

查看:103
本文介绍了如何生成具有无价值属性的DOM节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试生成这样的节点:

 < div layout horizo​​ntal>< / div> 

使用DOM API。



m尝试这样的事情:

  var d = document.createElement(div); 
d.setAttribute(...);

但从未真正设法生成我想要的。任何想法如何做?



谢谢。

解决方案

您的问题的主题反映了这里的基本混乱。没有像无价值属性这样的东西。在HTML中只能有一个句法方便的方式来编写一个没有特定值的属性,只要属性的存在意味着什么,无论其价值如何。



所以你可以写< input required> < input required = true'> < input required ='false'> < input required ='notrequired' < / code>,这意味着完全相同的东西(这是输入 需要)。



当你尝试打印,用你的话来说,无价值的属性,负责打印的人可能会选择对你好,而不是显示空字符串来表示属性。但是,这不会改变属性确实具有空字符串的值,因为可以通过使用 elt.getAttribute 并直接检查属性值来看到这一点。



例如,如果我创建一个新的div,那么将它的内部HTML设置为< input required => ,然后某些环境(如Chrome devtools)会将元素显示为< input required> 而不是< input required = > 。换句话说,这只是语法糖,换句话说就是。



一旦我创建了一个具有无价值属性的元素,实际上一个空字符串,也许我可以通过将属性值设置为 null 来实现真实的不。 elt.setAttribute('required',null)只将属性值设置为字符串null

请注意,DOM规范指定了指定的属性 code>在 Attr 对象。但是,据我所知,这似乎没有被使用,在这个意义上,由 document.createAttribute 创建的属性总是得到指定设置为true,通过解析HTML创建的属性,如 x.innerHTML ='< input required>'; 始终获取指定设置为true等。而指定的标记为只读,所以没有办法检查将其设置为false的效果无论如何,



无论如何,你的目标是什么?为什么你认为你需要这样做?


I'm trying to generate nodes like this:

<div layout horizontal></div>

using DOM API.

I'm trying something like this:

var d = document.createElement("div");
d.setAttribute(...);

but never really managed to generate what I want. Any ideas how to do this?

Thanks.

解决方案

The subject of your question reflects the basic confusion here. There is no such things as a "valueless attribute". There is merely a syntactic convenience available in HTML to write an attribute with no specific value, in cases where the mere presence of the attribute means something, whatever its value.

So you could write <input required> or <input required='true'> or <input required='false'> or <input required='notrequired'> and it would mean exactly the same thing (which is that input is required).

When you try to "print", in your words, the "valueless" attribute, whoever is responsible for the "printing" may choose to "be nice to you" and represent the attribute with no value, rather than showing the empty string. However, this does not change the fact that the attribute does have a value of the empty string, as can be seen easily by taking elt.getAttribute and examining the attribute value directly.

For instance, if I create a new div, then set its inner HTML to <input required="">, then certain environments, such as Chrome devtools, will show the element as <input required> rather than <input required="">. It's just syntactic sugar on both the way in and the way out, in other words.

Once I've created an element with a "valueless" attribute, which is actually an empty string, perhaps I can then make it truly valueless by setting the attribute value to null? Nope. elt.setAttribute('required', null) merely sets the attribute value to the string "null". Same for undefined.

Note that the DOM specification specifies an attribute of specified on Attr objects. However, as far as I can tell, this does not seem to be used, in the sense that, an attribute created by document.createAttribute always gets specified set to true, an attribute created by parsing HTML as in x.innerHTML = '<input required>'; always gets specified set to true, etc. And specified is marked readonly, so there's no way to examine what the effects of setting it to false would be anyway.

Anyway, what is your objective here? Why do you think you need to do this?

这篇关于如何生成具有无价值属性的DOM节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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