Jquery 删除链接开头的点和斜线
我有这样一个html:Jquery 删除链接开头的点和斜线,jquery,html,Jquery,Html,我有这样一个html: <table id="mytable"> <tr> <td><a href="../directory/place/main/?qs=sample">Some Text</a></td> </tr> </table> 如何使用JQUERY删除href属性上的../呢?删除“../”就是您想要做的事 var a = $('a')
<table id="mytable">
<tr>
<td><a href="../directory/place/main/?qs=sample">Some Text</a></td>
</tr>
</table>
如何使用JQUERY删除href属性上的../呢?删除“../”就是您想要做的事
var a = $('a'); // Selector for anchor
var href = a.attr('href');
href = href.replace('../', '');
a.attr('href', href);
id应该是唯一的(每页只有一个)。
只要将代码定义为类,它就可以正常工作。
$(函数(){
$('.a')。每个(函数(){
var src=$(this.attr('href');
如果(src.indexOf('..')==0){
this.src=src.replace(/\.\.\//g,'http://sample.com/');
}
});
警报($('.a')[0].src);
});
试试这样的方法
jQuery(function(){
jQuery('#mytable a').each(function(){
var str = this.href;
this.href = str.substring(3);
});
})
如果您没有../在每个href中,您可以尝试下面的代码
var str = this.href;
this.href = str.replace('../', '');
请告诉我们您到目前为止都尝试了什么。实际上,我将至少替换空间的这个相对路径(../)或类似的链接。我使用了这段代码,但没有任何效果。
$('a')。每个(function(){var src=$(this.attr('href');if(src.indexOf('.')==0){this.src=src.replace(//\.\.\//g,'http://sample.com/'); } });代码>id应该是唯一的。通过你的每一个我知道你有不止一个id为“a”的链接。你这样做的想法是什么?我有一个表id。。锚链没有等级。我将编辑上面的html示例。它从一开始就包含3个字符串。。结果是这样的:p://localhost/directory/place/main/?qs=samplethis这个命令行有效,我只需添加另一个命令行,并替换我要替换的链接var str=this.href;this.href=str.replace(“../”,“”);this.href=str.replace('http://localhost', 'http://www.sample.com/');代码>非常感谢。。其他答案也可能有用。。谢谢大家的贡献。。
var str = this.href;
this.href = str.replace('../', '');