JQuery 中的点和哈希符号是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2860394/
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
What do dot and hash symbols mean in JQuery?
提问by user327712
I feel confused of the dot and hash symbols in the following example:
我对以下示例中的点和哈希符号感到困惑:
<DIV ID="row">
<DIV ID="c1">
<Input type="radio" name="testing" id="testing" VALUE="1">testing1
</DIV>
</DIV>
Code 1:
代码 1:
$('#row DIV').mouseover(function(){
$('#row DIV').addClass('testing');
});
Code 2
代码 2
$('.row div').mouseover(function(){
$(this).addClass('testing');
});?
Codes 1 and 2 look very similar, and so it makes me so confused that
when I should use ".row div" to refer to a specific DIV instead of using "#row div" ?
代码 1 和代码 2 看起来非常相似,所以我很困惑,
什么时候应该使用“.row div”来引用特定的 DIV 而不是使用“#row div”?
回答by Jeff Schumacher
The hash (#) specifies to select elements by their ID's
散列 (#) 指定按 ID 选择元素
The dot (.) specifies to select elements by their classname
点 (.) 指定按类名选择元素
You can read more about the selectors here: http://api.jquery.com/category/selectors/basic-css-selectors/
您可以在此处阅读有关选择器的更多信息:http: //api.jquery.com/category/selectors/basic-css-selectors/
回答by Matthew Jones
$('.row') will select any element with class="row"
$('.row') 将选择任何元素 class="row"
$('#row') will select the element with id=row
$('#row') 将选择元素 id=row
回答by Justin Ethier
These are CSS selectors.
这些是 CSS 选择器。
The hash symbol #
means that the element is an ID. So #row
would match <div id="row">
.
哈希符号#
表示该元素是一个 ID。所以#row
会匹配<div id="row">
。
Alternatively, the dot symbol .
means the element is a CSS class. So .row
would match <div class="row">
.
或者,点符号.
表示该元素是一个 CSS 类。所以.row
会匹配<div class="row">
。
There is more information over at W3C.
在W3C 上有更多信息。
回答by Chris Doggett
"." refers to a class, while "#" refers to IDs.
“。” 指的是一个类,而“#”指的是 ID。
<table id="table">
<tr class="odd"></tr>
<tr></tr>
<tr class="odd"></tr>
</table>
$("#table") would get the full table object, while $(".odd") would get everything with the class "odd". $("tr.odd") would only get table rows with that class.
$("#table") 将获得完整的表对象,而 $(".odd") 将获得类“odd”的所有内容。$("tr.odd") 只会获取该类的表行。
回答by EAMann
The .
specifies a classcalled "row." The #
specifies an idcalled "row."
该.
指定一类称为“行。” 该#
指定的ID叫“行。”