Html “此时元素 div 上不允许使用属性名称”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4962070/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"Attribute name not allowed on element div at this point"
提问by Thew
I am getting a W3V validator error that I can't understand:
我收到一个我无法理解的 W3V 验证器错误:
Line 31, Column 61: Attribute
name
not allowed on elementdiv
at this point.
第 31 行,第 61 列:此时
name
元素div
上不允许使用属性。
That is this row:
那是这一行:
<div name="message" class="jGrowl bottom-right errorGrowl"></div>
Full HTML:
完整的 HTML:
<!DOCTYPE html>
<html>
<head>
<title>jGrowl</title>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.9/jquery-ui.min.js"></script>
<script type="text/javascript" src="data/awo-jgrowl.js"></script>
<script type="text/javascript" src="data/shortcut.js"></script>
<link rel="stylesheet" type="text/css" href="data/awo-jgrowl.css">
<script type="text/javascript">
$(document).ready(function() {
$('div[name=message]').awomsg('Input message', {sticky: true});
});
shortcut.add("m",function() {
$('div[name=message]').awomsg('Input message', {sticky: true});
});
shortcut.add("h",function() {
alert('ur doin it wrong');
});
</script>
</head>
<body>
<div name="message" class="jGrowl bottom-right errorGrowl"></div>
</body>
</html>
采纳答案by Darin Dimitrov
The error message seems pretty self explanatory. You cannot have a name
attribute on a div
tag. So your code could look like this:
错误消息似乎很容易解释。标签name
上不能有属性div
。所以你的代码可能是这样的:
<div id="message" class="jGrowl bottom-right errorGrowl"></div>
and then use id selectors:
然后使用 id 选择器:
$('div#message')...
回答by Ross
I found some entry from:
我找到了一些条目:
Markup Validation Error: "Attribute name not allowed on element at this point" error #HTML5
标记验证错误:“此时元素上不允许使用属性名称”错误 #HTML5
Just in case you intend to define a custom attribute, you have to prepend the attribute with "data-
".
以防万一您打算定义自定义属性,您必须在属性前加上“ data-
”。
So in this case, name would be: data-name=""
.
所以在这种情况下,名称将是:data-name=""
。
And you can reference it by 'div[data-name="value"]'
.
您可以通过'div[data-name="value"]'
.
回答by Quentin
There is no name
attribute for div elements.
name
div 元素没有属性。
If you want to uniquely identify one, then use id
.
如果要唯一标识一个,请使用id
.
If you want to mark one as a member of a group, then use class
.
如果要将某个人标记为组成员,请使用class
。
The only place you can use a name
attribute (that hasn't been deprecated) is on form controls (input
, select
, textarea
and button
).
您可以使用的唯一的地方name
属性(尚未过时)是表单控件(input
,select
,textarea
和button
)。
回答by Tom Boutell
This is a late response, but since this page just came up in a search:
这是一个迟到的回复,但由于此页面刚刚出现在搜索中:
Since the name attribute is not permitted on certain elements and has special significance in forms which you may not want, but any attribute name starting with "data-" is acceptable to use for purposes of your own, I recommend using the "data-name" attribute, like this:
由于 name 属性在某些元素上是不允许的,并且在您可能不想要的形式中具有特殊意义,但是任何以“data-”开头的属性名称都可以用于您自己的目的,我建议使用“data-name” " 属性,像这样:
<div data-name="message" class="jGrowl bottom-right errorGrowl"></div>
You can then write:
然后你可以写:
$('[data-name="message"]').text("Here is a new message!");
And otherwise manipulate the div via jQuery.
否则通过 jQuery 操作 div。
The use of data attributes has the virtue that it is unlikely to conflict with what your frontend designers may be doing with IDs and class names for CSS purposes.
使用数据属性的优点是,它不太可能与您的前端设计人员为 CSS 目的使用 ID 和类名所做的事情发生冲突。
In our office we have an understanding that IDs and classes are reserved for CSS, and JavaScript developers should leave them alone. Conversely, frontend designers are welcome to change the ID, classes or even element type of most things, provided that they don't mess with the data attributes.
在我们的办公室里,我们知道 ID 和类是为 CSS 保留的,JavaScript 开发人员应该把它们放在一边。相反,欢迎前端设计人员更改大多数事物的 ID、类甚至元素类型,前提是他们不要弄乱数据属性。
回答by Chris Baker
The name
attribute is not part of the specification for DIV elements. name
is, generally speaking, only valid on form elements.
该name
属性不是 DIV 元素规范的一部分。 name
一般而言,仅对表单元素有效。