[英]Get last part of the url
Trying to get the last part of the url in a pretty weird html structure.试图以非常奇怪的 html 结构获取 url 的最后一部分。 Don't ask why it's built that way.不要问为什么它是这样建造的。 There is a very good reason behind it.这背后有一个很好的理由。
The html looks like this html看起来像这样
<li class="lifilter"><input type="checkbox" class="filtercheck" id="Cheeks...">
<label for="Cheeks...">
<a href="/collections/cheeks">Cheeks</a>
</label>
</li>
and the js i'm trying to use和我正在尝试使用的 js
$('#Cheeks... label a').each(function(){
var lasturl = $(this).attr('href');
var urlsplit = url.split("/");
var finalvar = urlsplit[4];
$(this).addClass(finalvar);
});
edit: damn.. i can only post once every 90 minutes.编辑:该死的..我只能每 90 分钟发布一次。 here is updated question with updated html这是更新后的 html 问题
<li class="lifilter">
<input type="checkbox" class="filtercheck" id="Cheeks...">
<label for="Cheeks...">
<a href="/collections/cheeks" class="cheeks">Cheeks</a>
</label>
</li>
and the js code i'm trying to use (from a previous answer)以及我尝试使用的 js 代码(来自之前的答案)
$('.lifilter').each(function(){
$(this).find(".filtercheck").next('label').find('a').each(function(){
var lasturl = $(this).attr('href');
var urlsplit = lasturl.split("/");
console.log(urlsplit);
var finalvar = urlsplit.pop();
console.log('Adding class: ' + finalvar);
$(this).addClass(finalvar);
});
});
OK, so it appears no one here attempted to try the solution here before posting.好的,所以似乎没有人在发布之前尝试在这里尝试解决方案。
First things first cheeks...
.首先是cheeks...
This is a tricky ID to find (You have to escape the periods).这是一个很难找到的 ID(你必须避开句号)。 The label
is also not part of the internal html where ID is cheeks...
, so we need to find the adjacent element and look the a
anchor tag you're looking for. label
也不是内部 html 的一部分,其中 ID 是cheeks...
,因此我们需要找到相邻元素并查找您正在寻找a
锚标记。
Here's the code:这是代码:
$(document).ready(function() {
$('#Cheeks\\.\\.\\.').next('label').find('a').each(function(){
var lasturl = $(this).attr('href');
var urlsplit = lasturl.split("/");
console.log(urlsplit);
var finalvar = urlsplit.pop();
console.log('Adding class: ' + finalvar);
$(this).addClass(finalvar);
});
});
And here is a working jsfiddle with the solution.这是解决方案的有效jsfiddle。
keeping it simple like your code you'd do保持简单,就像你的代码一样
finalvar = urlsplit[urlsplit.length-1];
in case you don't want the base url as a valid return then:如果您不希望基本 url 作为有效返回,则:
finalvar = ( urlsplit.length > 1 ? urlsplit[urlsplit.length-1] : "" );
replace "" with your preferred error/default return将 "" 替换为您首选的错误/默认返回
you could also try to find the index of the last '/' and do a substring.你也可以尝试找到最后一个 '/' 的索引并做一个子字符串。
try this.尝试这个。
var URI = 'www.example.com/sub1/sub2/sub3/',
parts = URI.split('/'),
lastPart = parts.pop() == '' ? parts[parts.length - 1] : parts.pop();
//RESULT : "sub3"
You can extract the last section of a path (ie everything after the last /
) by using a regular expression:您可以使用正则表达式提取路径的最后一部分(即最后一个/
之后的所有内容):
text.replace(/.*\\//g, "")
This will remove all of the text before a slash, as well as the slash itself.这将删除斜杠之前的所有文本,以及斜杠本身。 You'll also notice that your selector wasn't matching any elements;您还会注意到您的选择器没有匹配任何元素; you're looking for labels nested within inputs, which doesn't match the html you posted (and isn't a valid DOM structure).您正在寻找嵌套在输入中的标签,该标签与您发布的 html 不匹配(并且不是有效的 DOM 结构)。 An appropriate selector would be .lifilter label a
, since the <label>
is within the <li>
.一个合适的选择器是.lifilter label a
,因为<label>
在<li>
。
$(function() { setTimeout(function() { $('.lifilter label a').each(function() { // strip everything up to and including the last forward slash var path = $(this).attr('href').replace(/.*\\//g, ""); $(this).addClass(path); }); }, 1500); });
a.cheeks:after { content: " (className = 'cheeks')"; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <li class="lifilter"> <input type="checkbox" class="filtercheck" id="Cheeks..."> <label for="Cheeks..."> <a href="/collections/cheeks">Cheeks</a> </label> </li>
if you want the last section of url for example activation code or id .You can try this.如果你想要 url 的最后一部分,例如激活码或id 。你可以试试这个。
var url = 'www.abc.com/code=12345',
parts = url.split('='),
lastPart = parts.pop()
//lastPart = 12345
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.