假设这是我的表
<table border="1" id="tbl">
<tr class="clsHead">
<td>
<a href="b.aspx">first row first cell</a>
</td>
<td>
<a href="b.aspx">first row second cell</a>
</td>
</tr>
<tr>
<td colspan="2">
<a href="">seconde row</a>
</td>
</tr>
</table>
我尝试了这个jquery
但没有按预期工作。我试图更改或修改具有类名 clsHead
$(document).ready(function () {
$(".clsHead").each(function () {
alert($(this).attr('href'));
$(this).attr('href',$(this).attr('href')+'&qString=1') ;
});
});
如果可能的话告诉我哪里弄错了。感谢
答案 0 :(得分:3)
您正在尝试获取/设置每个href
元素的.clsHead
属性,因为它们没有href
个属性,所以它显然无效。
您需要选择后代a
元素:
$(".clsHead a").each(function() {
$(this).attr('href', $(this).attr('href') + '&qString=1');
});
虽然只需访问href
的{{1}}属性:
this
或者没有jQuery:
$(".clsHead a").each(function() {
this.href = this.href + '&qString=1';
});
您可能需要将该逻辑包装在DOM加载的事件回调中:
var elements = document.querySelectorAll('.clsHead a');
Array.prototype.forEach.call(elements, function (el) {
el.href = el.href + '&qString=1';
});
答案 1 :(得分:0)
试试这个。
$(document).ready(function (){
$(".clsHead a").each(function ()
{
alert($(this).attr('href'));
$(this).attr('href',$(this).attr('href')+'&qString=1') ;
});
});