我有一个像这样的HTML:
<table id="mytable">
<tr>
<td><a href="../directory/place/main/?qs=sample">Some Text</a></td>
</tr>
</table>
如何使用JQUERY删除href属性上的../?
答案 0 :(得分:1)
尝试这样的事情
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('../', '');
答案 1 :(得分:0)
删除&#34; ../"你想做什么?
var a = $('a'); // Selector for anchor
var href = a.attr('href');
href = href.replace('../', '');
a.attr('href', href);
答案 2 :(得分:0)
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);
});