简体   繁体   中英

jQuery get href attr remove .html

I have the following jQuery:

$('.aboutUs a').live('click', function(e){
    e.preventDefault();

    var clickedElement = $(this).attr('href');
});

If I console.log the result I will get page.html. What I would like to is remove the .html but not sure how. Can anyone help? Many thanks.

You could just replace the .html with nothing:

var clickedElement = $(this).attr("href").replace(".html", "");

Note that if your string could potentially contain other instances of ".html" then the regex answer is better, because in that case, this will remove the first occurence rather than the last. But if that's not the case (and it sounds quite unlikely) then this should work fine.

您可以使用正则表达式从字符串末尾删除.html ,例如。

"page.html".replace(/.html$/,'')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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