包含空字符串的HTML属性不显示=运算符 [英] Html attribute containing empty string doesn't show = operator
问题描述
一个用例是在jQuery中:
One use case is in jQuery:
$select.append('<option value="">All</option>');
看起来它实际上在HTML中添加了元素:
It looks like it actually adds the element in HTML:
<option value>All</option>
相反,我要附加到元素上,以便为值提供一个空字符串:
Instead, what I want is to append to the element so that it gives an empty string to value:
<option value="">All</option>
为什么那不会发生?
推荐答案
它实际上在HTML中添加了元素
It actually add the element in HTML
不,不是.
它将元素添加到DOM,而不是HTML.
It adds the element to the DOM, not to the HTML.
使用浏览器的开发人员工具查看DOM时,它将使用类似HTML的语法显示.使用这种语法,将呈现一个 value
属性,该值是一个空字符串,而不包含 ="
部分.(看起来该语法的一部分实际上是有效的HTML,但是我在规范中没有找到允许该部分的部分.)
When you look at the DOM, using your browser's developer tools, it will displayed using HTML-like syntax. In this syntax, a value
attribute where the value is an empty string will be rendered without the =""
portion. (It is looking like that part of the syntax is actually valid HTML, but I haven't found the part in the spec which allows it.)
如果您使用的是Chrome浏览器(以Chrome为例),则可以右键单击其父元素,然后选择以HTML格式编辑",此时您将再次看到 ="
.
If you were (to use Chrome as an example), you could right click on its parent element and pick "Edit as HTML" at which point you would see the =""
again.
如果要提交包含select元素的表单,您将看到发送的数据为: select_name =
.这表明该值已正确设置为空字符串.如果不起作用,您将得到 select_name = All
,因为如果未设置任何值,则将元素的文本用作值.
If you were to submit the form containing the select element you would see that the data sent would be: select_name=
. This shows that the value was correctly set to an empty string. If it had not worked you would have got select_name=All
since the element's text is used as the value if no value is set.
这篇关于包含空字符串的HTML属性不显示=运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!