jQuery $(this.hash) 如何工作?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32772363/
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
How does $(this.hash) work?
提问by Zcela Anonymní
How $(this.hash) works in jQuery? I presupposed that this script should work like this - if I click to link with href tickets it will show div with id tickets. But it not works.
$(this.hash) 在 jQuery 中是如何工作的?我预先假设这个脚本应该像这样工作 - 如果我点击链接与 href 票证,它将显示带有 id 票证的 div。但它不起作用。
var search = $("#switcher").find("a"),
hotels = $("#find").children("div").hide();
search.on('click', function (e) {
$(this.hash).show()
e.preventDefault()
});
回答by Barmar
this.hash
reads the href
attribute of this
, and gets the part of the URL beginning with #
. So if the anchor looks like:
this.hash
读取 的href
属性this
,并获取以 开头的 URL 部分#
。所以如果锚看起来像:
<a href="someURL#foobar">
this.hash
will be #foobar
. When you then use $(this.hash).show()
, it's equivalent to doing $("#foobar").show()
, so it will show the element with id="foobar"
.
this.hash
将#foobar
。当您使用 时$(this.hash).show()
,它相当于执行$("#foobar").show()
,因此它将显示带有 的元素id="foobar"
。