jQuery的ATTR HREF,为什么不工作? [英] jQuery attr href, why isn't it working?

查看:203
本文介绍了jQuery的ATTR HREF,为什么不工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想code的下面一行应该很好地工作: $(1)ATTR(HREF','#Home');
对吧?

I thought the following line of code should work fine: $(".1").attr('href', '#Home'); Right?

但为什么是不是工作,当我与另一个jQuery脚本整合呢?

But why isn't it working when I integrate it with another jQuery script?

$(window).bind("load", function() {
    $('.1').click(function() {
    	$('.1').removeClass('tab1');
    	$('.2').removeClass('active2');
    	$('.3').removeClass('active3');
    	$('.4').removeClass('active4');

    	$('.1').addClass('active1');

    	$('.2').addClass('tab2');
    	$('.3').addClass('tab3');
    	$('.4').addClass('tab4');

    	$('#PortfolioMainContainer:visible').fadeOut("slow",function(){
    		$('#TextContent').load('Home.html', function() {
    		 	$(this).fadeIn("slow")
    		});
    		return false;
    	});	

    	if(!$(".1").hasClass("ActiveTab1")) {
    		$(".1").attr('href', '#Home');
    		$('#TextContent:visible').fadeOut("slow",function(){
    			$('#TextContent').load('Home.html', function() {
    				$(this).fadeIn("slow")
    			});
    			return false;
    		});
    	}
    	$(".1").addClass("ActiveTab1");

    	$(".2").removeClass("ActiveTab2");
    	$(".3").removeClass("ActiveTab3");
    	$(".4").removeClass("ActiveTab4");
    });
});

我要得到清晰的事情是当你点击与类.1那么URL必须改变的 http://www.websiteurl.com/#Home

任何人是否有一个想法如何得到它的工作?

Does anybody have an idea how to get it working?

推荐答案

我测试以下语句,它实际工作。

I tested the following statements and it actually works.

        $(function() {
            $("a").attr("href", "#123");
        });

如果我点击任何链接,位置实际上是在毫无疑问的头连着#123。

And if I click on any link, the location actually attached #123 at the end with no doubt.

我觉得你的问题可以,你的.1是不是连接到一个锚对象。在HTML规范中,只有超链接(和一些不相关的HTML标签)都是有href属性。这意味着,例如,你的 .1 实际上是一个< D​​IV CLASS =&GT'1'; ,那么,即使你把href属性,它不会有作为超链接的默认行为。在这种情况下,你应该programattically导航到指定的URL,如:

I think your problem could be, your ".1" is not attaching to an anchor object. In HTML spec, only hyperlink (and some not relevant html tags) are having "href" attribute. That means, for example, your .1 is actually a <div class='.1'>, then, even you put href attribute to it, it would not have any default behavior acting as "hyperlink". In this case, you should programattically navigate to designated url like:

$(".1").click(function(){
    window.location = "current/url" + "#home";
});

这篇关于jQuery的ATTR HREF,为什么不工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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