jQuery attr href,为什么不工作?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1068385/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 10:34:08  来源:igfitidea点击:

jQuery attr href, why isn't it working?

jqueryattributeshref

提问by Bas

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

我认为以下代码行应该可以正常工作:$(".1").attr('href', '#Home');对吗?

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?

有人知道如何让它工作吗?

回答by xandy

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. In HTML spec, only hyperlink (and some not relevant html tags) are having "href" attribute. That means, for example, your .1is 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”没有附加到锚点对象。在 HTML 规范中,只有超链接(和一些不相关的 html 标签)具有“href”属性。这意味着,例如,您.1实际上是 a <div class='.1'>,那么,即使您为其添加了 href 属性,它也不会有任何充当“超链接”的默认行为。在这种情况下,您应该以编程方式导航到指定的 url,如:

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

回答by googletorp

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'. 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.

问题是 document.URL 将获得包含磅和所有内容的 url,因此如果您在 example.com/#work 上,docuement.URL 将返回“example.com/#work”。所以你可能想要做一些检查,或者如果你知道你在这个脚本的静态 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 类,所以它不应该进入代码的那部分,除非它已经有那个类。

回答by rahul

You have to append the current locationto the href attribute.

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

回答by Ben

You need to add the namespace $("a").attr("xlink:href", "#123");

您需要添加命名空间 $("a").attr("xlink:href", "#123");

回答by Narek Ghazaryan

If your teg in iframe , you can't do anything by jquery try do something like this console.log($(".1").html()); and you will see null

如果您的 teg 在 iframe 中,您将无法通过 jquery 执行任何操作,请尝试执行这样的操作 console.log($(".1").html()); 你会看到 null