首页 新闻 会员 周边

$(this.hash)是什么意思?

0
悬赏园豆:15 [已解决问题] 解决于 2011-10-30 18:30

今天看jquery的资料,看到一句$(this.hash),不知道是什么意思,找了api,没有。百度一下,没有。哪位大虾知道啊,指点一下小弟,谢谢了

问题补充:

http://jqueryfordesigners.com/jquery-tabs/代码出处,大家帮忙看看,谢谢啦

$(function(){
   
var tabs =[];
   
var tabContainers =[];
    $
('ul.tabs a').each(function(){
     
// note that this only compares the pathname, not the entire url
     
// which actually may be required for a more terse solution.
       
if(this.pathname == window.location.pathname){
            tabs
.push(this);
            tabContainers
.push($(this.hash).get(0));
       
}
   
});
   
    $
(tabs).click(function(){
       
// hide all tabs
        $
(tabContainers).hide().filter(this.hash).show();
       
       
// set up the selected class
        $
(tabs).removeClass('selected');
        $
(this).addClass('selected');
       
       
returnfalse;
   
});
});
司南Thinan的主页 司南Thinan | 初学一级 | 园豆:187
提问于:2011-10-28 15:42
< >
分享
最佳答案
2

看上下文

 $('ul.tabs a')这是链接,该dom元素本身就有pathname,hash这些属性。
详情请查看window.location对象,跟jquery没有关系
什么叫hash,见文章http://www.ruanyifeng.com/blog/2011/03/url_hash.html 
收获园豆:15
码不能停 | 菜鸟二级 |园豆:201 | 2011-10-29 22:46

谢谢,懂一点点,但是大部分还是模糊

if(this.pathname == window.location.pathname){//这个无论我怎么点,ie下都是false,火狐下都是true,不懂作者是要干什么
            tabs
.push(this);
            tabContainers
.push($(this.hash).get(0));
       
}
你你能帮我解释一下吗?
司南Thinan | 园豆:187 (初学一级) | 2011-10-30 11:36

@大凯iceblove: 

的确有这个问题。原因是在本地环境下调试。

 
<a title="" href="http://www.cnblogs.com/flowerszhong/#web_location" id="cnblogs">flowerszhong</a>
<a title="" href="#local_location" id="currentPage">flowerszhong</a>

<script>
$(
function(){
$(
"#cnblogs,#currentPage").click(function(){
alert(
"location:"+this.href+";\n pathname:"+this.pathname+"\n window.location.pathname:"+ window.location.pathname +"\n hash: "+this.hash);
returnfalse;})
})
</script>

当你的链接的href值不是以http://开头,即默认为本地路径,你又是在本地调试,ie跟ff返回的pathname不一样。服务器环境下是一样的。

jquery-tabs判断if(this.pathname == window.location.pathname)是严谨的态度,但可以去掉。

码不能停 | 园豆:201 (菜鸟二级) | 2011-10-30 15:55

@繁花:

这个东西写得好厉害呀,如果让我写,不一定用什么笨法呢!谢谢啦,你给的那篇文章挺好!

司南Thinan | 园豆:187 (初学一级) | 2011-10-30 18:36
其他回答(6)
0

是不是哈希值什么的

喬喬AI | 园豆:996 (小虾三级) | 2011-10-28 16:01
0

this 指的是当前对象,hash 应该一个哈希键值对的js函数

紫炁星 | 园豆:57 (初学一级) | 2011-10-28 17:59
0

你在什么地方看到的?

顾晓北 | 园豆:10844 (专家六级) | 2011-10-29 08:29

http://jqueryfordesigners.com/jquery-tabs/

在这个小例子看到的

支持(0) 反对(0) 司南Thinan | 园豆:187 (初学一级) | 2011-10-29 09:58
0

不知道你的context,所以无法确定this引用的是什么。document.location.hash?

Davindai | 园豆:205 (菜鸟二级) | 2011-10-29 14:09
0

将js hash对象转换为jquery对象

huzl | 园豆:281 (菜鸟二级) | 2011-10-29 15:29
0

我在w3school电子书中没有找到hash这个属性和方法
楼至要给我们jquery调用的版本和是否调用别的jquery别的方法
this指的是对象本身是一个DOM对象

唯吴独尊 | 园豆:707 (小虾三级) | 2011-10-29 21:41

版本是1.2.3 ,没有调用其他方法,是不是过时了??

支持(0) 反对(0) 司南Thinan | 园豆:187 (初学一级) | 2011-10-29 22:19
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册