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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 16:55:16  来源:igfitidea点击:

How does $(this.hash) work?

jqueryhash

提问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.hashreads the hrefattribute 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.hashwill 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"