$(this.hash)如何工作? [英] How does $(this.hash) work?

查看:109
本文介绍了$(this.hash)如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$(this.hash)在jQuery中如何工作?我以为这个脚本应该像这样工作-如果我单击以链接href票证,它将显示带有id票证的div.但这是行不通的.

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.

var search = $("#switcher").find("a"),
    hotels = $("#find").children("div").hide();

search.on('click', function (e) {

  $(this.hash).show()
  e.preventDefault()
});

推荐答案

this.hash读取thishref属性,并获取以#开头的URL部分.因此,如果锚点看起来像:

this.hash reads the href attribute of this, and gets the part of the URL beginning with #. So if the anchor looks like:

<a href="someURL#foobar">

this.hash将为#foobar.然后,当您使用$(this.hash).show()时,它等效于执行$("#foobar").show(),因此它将显示带有id="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)如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆