上开始削减我有一个这样的HTML:删除点,并在链接
<table id="mytable">
<tr>
<td><a href="../directory/place/main/?qs=sample">Some Text</a></td>
</tr>
</table>
我如何删除上使用jQuery href属性的../?
上开始削减我有一个这样的HTML:删除点,并在链接
<table id="mytable">
<tr>
<td><a href="../directory/place/main/?qs=sample">Some Text</a></td>
</tr>
</table>
我如何删除上使用jQuery href属性的../?
尝试这样的事情
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('../', '');
它从一开始就忽略3个字符串..结果已经像这样: p:// localhost/directory/place/main /?qs = sample – Onimax
这个工作我只是添加另一个将http:// localhost替换为我想替换的链接的命令行 'var str = this.href; this.href = str.replace('../',''); this.href = str.replace('http:// localhost','http://www.sample.com/');' – Onimax
非常感谢..其他答案也可能有效。贡献.. – Onimax
删除“../”是什么,你只是想干什么?
var a = $('a'); // Selector for anchor
var href = a.attr('href');
href = href.replace('../', '');
a.attr('href', href);
id应该是唯一的(每页只有一个)。 你的代码,只要你把它定义为一个类,而不是正常工作。 http://jsfiddle.net/QsgPT/3/
<a href="../directory/place/main/?qs=sample" class="a">Test</a>
$(function(){
$('.a').each(function(){
var src = $(this).attr('href');
if (src.indexOf('..') === 0){
this.src = src.replace(/\.\.\//g,'http://sample.com/');
}
});
alert($('.a')[0].src);
});
我有一个表ID ..和锚链接没有类。生病编辑上面的html样本.. – Onimax
请告诉我们你尝试过什么到现在。 – Dementic
其实我即将取代至少这个相对路径(../)空间或类似的链接我使用此代码,但无济于事 '$('a')。each( function(){ var src = $(本).attr( 'href' 属性); \t如果( '' src.indexOf()=== 0){ this.src = src.replace(/\.\.\//克, 'http://sample.com/'); \t \t} });' – Onimax
的ID应该是唯一的。通过你的'每一个'我明白你有更多的一个链接与ID'A'。 – Dementic