简体   繁体   English

jQuery attr href,为什么不工作?

[英]jQuery attr href, why isn't it working?

I thought the following line of code should work fine: $(".1").attr('href', '#Home'); 我认为下面的代码行应该可以正常工作: $(".1").attr('href', '#Home'); Right? 对?

But why isn't it working when I integrate it with another jQuery script? 但是当我将它与另一个jQuery脚本集成时,为什么它不起作用?

$(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");
    });
});

The thing I want to get clear is when you click on the div with the class .1 then the URL has to change to http://www.websiteurl.com/#Home 我想要清楚的是当你点击带有类.1的div时,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");
        });

And if I click on any link, the location actually attached #123 at the end with no doubt. 如果我点击任何链接,毫无疑问,该位置实际附加了#123。

I think your problem could be, your ".1" is not attaching to an anchor object. 我认为你的问题可能是,你的“.1”没有附加到一个锚对象。 In HTML spec, only hyperlink (and some not relevant html tags) are having "href" attribute. 在HTML规范中,只有超链接(和一些不相关的html标签)具有“href”属性。 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". 这意味着,例如,你的.1实际上是一个<div class='.1'> ,那么,即使你把href属性赋予它,它也没有任何默认行为充当“超链接”。 In this case, you should programattically navigate to designated url like: 在这种情况下,您应该以编程方式导航到指定的URL,如:

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

You can use document.URL to get the current location, so 您可以使用document.URL获取当前位置,因此

$(".1").attr('href', document.URL + '#Home');

The thing is that document.URL will get the url with pounds and everything, so if you're on example.com/#work, docuement.URL would return 'example.com/#work'. 问题是document.URL将获得带有磅和所有内容的URL,因此如果您使用example.com/#work,docuement.URL将返回'example.com/#work'。 So you might want to do some checking, or if you know that you are on a static url for this script, you can just hardcode the url. 因此,您可能需要进行一些检查,或者如果您知道自己在此脚本的静态URL上,则可以对该网址进行硬编码。

One other thing, I can see that you are adding the class ActiveTab1 after checking for it, so it shouldn't go into that portion of the code, unless it already have that class. 另外一件事,我可以看到你在检查它之后添加了ActiveTab1类,所以它不应该进入代码的那一部分,除非它已经有了那个类。

您必须将当前位置附加到href属性。

您需要添加名称空间$(“a”)。attr(“xlink:href”,“#123”);

If your teg in iframe , you can't do anything by jquery try do something like this console.log($(".1").html()); 如果你在iframe中的teg,你不能通过jquery做任何事情尝试做这样的console.log($(“。1”)。html()); and you will see null 你会看到null

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM