开发者

Deleting part of href using JQuery

Hi I have few bookmarks in the CMS:

 <li><a href="#online">Chat Online</a> </li>
 <li><a href="#termsOfService">Terms of Service</a> </li>

But CMS is adding some url before # in the link which breaks my functionality. Is there any way to delete everything before # in开发者_如何学JAVA a link using Jquery. Thanks for the help


You could write a regular expression to remove everything before the hash in the href attribute. I'd suggest doing it on the server-side, but if you must use jQuery it might look something like this:

$('li a').each(function () {
    var href = $(this).attr('href').replace(/.*(#.*)$/, "$1");
    $(this).attr('href', href);
});


These elements have an id? If they do, you could do something like this, being XXX the internal id of each element

$(document).ready(function() {
    var href = $('#XXX').attr('href');
    href = href.split('#')[1]; 
    $('#XXX').attr('href', href);
});

If you wish to apply this to every anchor in you page, then:

$(document).ready(function() {
    $('a').each(function () {
        var href = $(this).attr('href');
        href = href.split('#')[1]; 
        $(this).attr('href', href);
    });
});

Hope this helps


Try this

Working demo

$(function(){
    $("a").each(function(){
        if(this.href && (this.href.indexOf("#") != -1)){
           this.href = this.href.split("#")[1];
        }
    });
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜