简体   繁体   English

在锚点标签的href属性中删除#

[英]remove # inside a href attribute of an anchor tag how

im making a tab and the concept is when a person click on the tab menu, jquery will then check on the href attribute of that anchor tag (tab menu) and remove # inside it and leaves the rest attr content of that attribute (href). 我正在制作一个标签,其概念是当一个人单击标签菜单时,jQuery将检查该锚标记的href属性(标签菜单)并删除其中的#号,并保留该属性的其余attr内容(href) 。 for example 例如

<a href="#home" class="tabmenu">Tab 1</a><a href="#inventory" class="tabmenu>Tab 2</a>
<div id="home" class="tab">content of tab 1 here</div>
<div id="inventory" class="tab">content of tab 2 here</div>

so when one of the tab menu is clicked. 因此,当点击选项卡菜单之一时。 jquery will remove the # of that anchor tag href attribute, therefore the href attribute will be this href="home" (if tab 1 is click) and then jquery will hide all tab content div first (.tab) and then show the content tab that has #home (if tab 1 is click). jQuery会删除该锚标记href属性的#,因此href属性将是这个href =“ home”(如果单击选项卡1),然后jQuery将首先隐藏所有选项卡内容div(.tab),然后显示内容具有#home的标签(如果单击了标签1)。

so the script concept will somehow look like this: 因此脚本概念将看起来像这样:

$(document).ready(function(){
    $('.tabmenu').click(function(){
        //get the anchor tag attr
        //remove # inside this anchor tag href attribute and the remaining content will be put in a variable
        //hide all tab content first
        //get the stored variable and then show the tab content that match of that stored variable e.g. (home or inventory).
    });
});

any help would be greatly appreciated. 任何帮助将不胜感激。 thank you! 谢谢!

Not sure why you want to change the href attribute, it's not even necessary for what you're doing, but maybe you need this for something else, so here you go: 不确定为什么要更改href属性,这甚至对于您正在做的事情都不是必需的,但是也许您还需要其他属性,因此在这里:

$(document).ready(function(){
    $('.tabmenu').click(function(){

        //get the anchor tag attr
        var href = $(this).attr('href');

        //remove # inside this anchor tag href attribute and the remaining content will be put in a variable
        if (href[0] == '#') {
            this.attr('href') = href.substring(1);
        }

        //hide all tab content first
        $('.tab').hide();

        //get the stored variable and then show the tab content that match of that stored variable e.g. (home or inventory).
        $(href).show();
    });
});

Please be aware that this code only works once for every link since we change the href attribute. 请注意,由于我们更改了href属性,因此该代码仅对每个链接起作用一次。 I can modify it to work with the changed attribute if you want to. 如果需要,我可以对其进行修改以与更改后的属性一起使用。

If you want the show only the matching content when the user click the tabs, you can use this code: 如果要在用户单击选项卡时仅显示匹配的内容,则可以使用以下代码:

$(document).ready(function(){    
    $('.tabmenu').click(function(){
        //hide all:
        $(".tab").hide();
        //show clicked tab:
        $("#" + $(this).attr('href').substring(1)).show();
    });
});

working fiddle: http://jsfiddle.net/x29gn8yo/ 工作提琴: http : //jsfiddle.net/x29gn8yo/

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

相关问题 在锚标签的href属性内执行javascript - executing javascript inside href attribute of anchor tag 为什么锚标记在href属性中指定的URL中删除制表符? - Why does anchor tag remove tab character in URL specified inside href attribute? 如何在加载后或加载页面时使用 java 脚本或 jquery 从锚标记中删除 href 属性 - How to remove href attribute from anchor tag after loading or while loading the page by using java script or jquery 在锚标记的 href 属性中使用 JavaScript 变量 - Using JavaScript variable inside an anchor tag's href attribute 如何引用在href属性中包含锚标记的引用 - How to reference containing anchor tag in it's href attribute 如何在锚标记内使用 Javascript 变量作为 href ? - How can I use a Javascript variable as href inside an anchor tag? 锚标记的数据绑定href属性 - Data-bind href attribute for anchor tag “javascript:;”表示HTML锚标记中的href属性 - “javascript:;” for href attribute in HTML anchor tag 如何将锚标记的 id 附加到 jquery 中的新 href 并替换页面中的 href 属性? - How to append the id of an anchor tag to a new href in jquery and replace the href attribute in a page? 如何使用javascript选择嵌套在表的tbody内的tr内的所有锚标记href URL - how can I select all anchor tag href URL that is nested inside a tr inside tbody of a table with javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM