我目前有一个博客,内容包含div。
我有以下脚本,它返回博客中的所有a href
标记。
function (){
var tags = [];
var count = $(".blog-featured").children().length;
for(var i=0; i<count; i++){
tags.push($('.blog-featured').children().eq(i).find('a').attr('href'));
}
return tags;
}
这将返回一系列URL,如下所示[undefined,www.test.com.au / product / url / 60145675?product / computer,www.test.com.au / product / url / 6014 8796 / test /产品]
我想将此数组操作为:
删除可能发生的任何空格(不确定原因,但脚本返回带空格的网址)
删除“6”之前的任何内容以及8之后的任何内容(如果空格未被删除,则删除9个)字符产品编号
删除所有未定义的值。
所以最终的数组看起来像[60145675,60148796]。
答案 0 :(得分:0)
在将其推入数组之前,只需拆分href并替换任何空格:
例如:www.test.com.au/product/url/6014 8796 / test / products
var loc =location.href;
var locPortions=loc.split("/");//splits the href at each "/";
var num=locPortions[3].replace(/ /g,"");//gets the desired portion of the href and replaces any spaces with no character - removing the spaces;
tags.push(num);//gives 60148796 out of the original href;
或将其放入原始功能: 编辑 - 修改下面的函数以在locPortions开始时检查“6”。
function (){
var tags = [];
var count = $(".blog-featured").children().length;
for(i=0; i<count; i++){
var loc = $('.blog-featured').children().eq(i).find('a').attr('href');
var locPortions=loc.split("/");
for (ii=0;ii<locPortions.length;ii++){
if(locPortions[ii].charAt(0)=="6")
{
var num=locPortions[ii].replace(/ /g,"");}
tags.push(num);
}
}
return tags;
}