[英]jquery attr('href') not working
Update, problem solved. 更新,问题已解决。 The issue was with a function that I was calling after trying to change the HREF, which was essentially changing it back.
问题出在我试图更改HREF(实际上是将其变回)之后调用的函数。
Using jQuery, I am trying to change the HREF attribute of a series of links based on the hash contained in the URL, but the attribute simply is not changing, it's being changed to an empty string. 使用jQuery,我试图根据URL中包含的哈希值更改一系列链接的HREF属性,但是该属性只是没有变化,而是被更改为空字符串。
Can anyone see what I'm doing wrong here? 有人可以在这里看到我在做什么错吗? Why isn't it working?
为什么不起作用?
http://fwy.pagodabox.com/categories/sculptures/#grid http://fwy.pagodabox.com/categories/sculptures/#grid
The links in question are 有问题的链接是
All Exhibitions Installations Objects Prints Sculptures
所有展览装置物件印刷品雕塑
In the secondary nav 在二级导航中
function navHash($navlinks, hashtxt) {
// loop through specified links
$navlinks.each(function(){
var $me = $j(this),
myhref,
index;
// Does this link have an href… if not move on
if( typeof $me.attr('href') === "undefined" )
return false;
myhref = $me.attr('href');
index = myhref.indexOf('#');
// if my href doesn't have the specified hash text, add it, else remove it
if(myhref.indexOf(hashtxt) === -1) {
$me.attr("href", hashtxt);
} else {
$me.attr("href", myhref.substring(0, index));
}
});
}
$j(document).ready(function($){
var $navlinks = $j('.sub-nav li:not(.views) a');
if(window.location.hash == '#grid') {
navHash($navlinks, '#grid');
$('.views .ic-grid').click();
}
// so on...
Thank you!!! 谢谢!!!
If you return false
here you will stop looping. 如果您在此处返回
false
,则将停止循环。
if(typeof $me.attr('href') === "undefined")
return false;
Return true
to continue looping. 返回
true
继续循环。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.