简体   繁体   English

jQuery get href attr remove .html

[英]jQuery get href attr remove .html

I have the following jQuery: 我有以下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. 如果我console.log结果我将获得page.html。 What I would like to is remove the .html but not sure how. 我想要的是删除.html但不确定如何。 Can anyone help? 有人可以帮忙吗? Many thanks. 非常感谢。

You could just replace the .html with nothing: 你可以直接replace .html

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. 请注意,如果您的字符串可能包含“.html”的其他实例,则正则表达式的答案更好,因为在这种情况下,这将删除第一个出现而不是最后出现。 But if that's not the case (and it sounds quite unlikely) then this should work fine. 但如果情况并非如此(听起来不太可能),那么这应该可行。

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

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

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

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