这个主题有很多可用但我找不到任何能回答我问题的内容......如果你发现这个多余的话,请礼貌地说出来而不是指责我。我已经非常努力了!
目标很简单。我有一系列div,其中一些包含锚点。我正在尝试将窗口的哈希值与div的锚子项的title属性进行比较,然后单击匹配的锚点的父级。
我的下面的函数记录了正确的锚子项数组,但我没有看到点击结果。我确定我错过了什么。
感谢您的帮助。
<div class="box"><img src="#" /></div>
<div class="box"><a href="#" title="blah"><img src="#" /></a></div>
<div class="box"><img src="#" /></div>
if(window.location.hash) {
var hash = window.location.hash.substring(1),
box = $('.box').children('a').each(function(){
$(this).attr('title');
});
if (box.attr('title') == hash) {
$(this).parent().click();
}
}
答案 0 :(得分:1)
从你在那里做的事情看来,$(this).attr('title')
没有做任何事情 - 它只是返回一个值,并没有做任何事情。
如果是我,我会这样做:
if(window.location.hash) {
var hash = window.location.hash.substring(1),
boxes = [];
$('.box').children('a').each(function(){
if ($(this).attr('title') == hash) {
boxes.push(this); // OR:
$(this).parents("div.box").click();
}
});
// Or, you can iterate through your boxes array and perform more validation here.
}