当元素有多个 id 时匹配
我正在循环浏览表单并显示与我选择的 ID 匹配的内容。问题是某些 div 包含多个 id,在这种情况下它会停止工作。有什么想法吗?谢谢。
Jquery 代码:
$('#myForm').find('div').each(function() {
var myId = $(this).attr('id');
/* This will work */
if (myId == "Select1"){
$(this).removeClass("hideMe");
$(this).addClass("showMe");
}
/* This does not work */
else if (myId == "Select4"){
$(this).removeClass("hideMe");
$(this).addClass("showMe");
}
else{}
});
HTML 代码:
<div class="hideMe" id="Select1">
<p>Some Content</p>
</div>
<div class="hideMe" id="Select2 Select3 Select4 Select5">
<p>Some Content</p>
</div>
I'm looping through a form and showing content that matches my selected id's. The problem is that some divs contain more than one id in which case it stops working. Any ideas? Thanks.
Jquery Code:
$('#myForm').find('div').each(function() {
var myId = $(this).attr('id');
/* This will work */
if (myId == "Select1"){
$(this).removeClass("hideMe");
$(this).addClass("showMe");
}
/* This does not work */
else if (myId == "Select4"){
$(this).removeClass("hideMe");
$(this).addClass("showMe");
}
else{}
});
HTML Code:
<div class="hideMe" id="Select1">
<p>Some Content</p>
</div>
<div class="hideMe" id="Select2 Select3 Select4 Select5">
<p>Some Content</p>
</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
不存在“多个 id”这样的事情。
https://developer.mozilla.org/en/XUL/Attribute/id
根据标准,
id
属性中的任何字符串数据都被视为值的一部分。ID 和 NAME 令牌必须以字母 ([A-Za-z]) 开头,后面可以跟任意数量的字母、数字 ([0-9])、连字符 ("-")、下划线 (" _”)、冒号(“:”)和句点(“.”)
。参考:http://www.w3.org/TR/REC -html40/types.html#type-name
不过还有另一种方法!您可以拥有各种类名,并且可以使用 jQuery 通过类名获取元素。
HTML
Javascript
其中的
[0]
部分是因为当您通过类名获取元素时,可以一些。 jQuery 选择器返回一个数组,因此您只需获取第一个数组。There is no such thing as "multiple ids".
https://developer.mozilla.org/en/XUL/Attribute/id
According to the standard, any string data within the
id
property is regarded as a part of the value.ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".")
.reference: http://www.w3.org/TR/REC-html40/types.html#type-name
There's another way, though! You can have all sorts of class names, and you can use jQuery to grab an element by class name.
HTML
Javascript
The
[0]
part of that is because when you get elements by class name, there can be several. The jQuery selector returns an array, so you're just grabbing the first one.你不能有多个 id。但是,如果您愿意,您可以选择多个课程。
You can't have multiple ids. However, you can have multiple classes if you wish.
拥有多个 ID 是无效的 - 浏览器会将 id="Select2 Select3 Select4 Select5" 视为单个字符串,但该字符串将无效,因为它包含空格。
来自 HTML 数据类型规范:http://www.w3。 org/TR/REC-html40/types.html#type-name
我认为您应该为此使用类。
It's not valid to have multiple ID's - the browser will see id="Select2 Select3 Select4 Select5" as a single string, but that string will be invalid because it contains spaces.
From the HTML data types spec: http://www.w3.org/TR/REC-html40/types.html#type-name
You should use classes for this, I think.
ID唯一并且元素只能有1个ID!
改用多个类。
ID's are unique and elements can only have 1 ID!
Use multiple classes instead.
一个元素不应该有多个唯一标识符,这就是它实际上被称为 id 的原因:为了将它与所有其他元素进行识别。无论如何,您必须测试
myId
是否包含 Select4,而不是测试相等性。An element shouldn't have more than one unique identifier that's why it's actually called an id: to identify it against all others. Anyway, you have to test if
myId
contains Select4 rather than testing the equality.