在一个table中,已知按钮id值,遍历tr时,根据这个id设置按钮文本值。
以下方式设置失败了,不知正确写法是什么?
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="/jquery/jquery.js"></script>
</head>
<body>
<table>
<tr>
<td id="up1"><button id='btn11'>aaa</button></td>
<td id="up2"><button id='btn12'>bbb</button></td>
<tr>
</table>
<script>
$("table tr").each(function(){
var btnID='btn12';//已经button id
var temp=$(btnID).Val('cccc'); //赋值失败
alert($(btnID).Text());//无弹出数据
})
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="/jquery/jquery.js"></script>
</head>
<body>
<table>
<tr>
<td id="up1"><button id='btn11'>aaa</button></td>
<td id="up2"><button id='btn12'>bbb</button></td>
</tr>
</table>
<script>
$("table tr").each(function(){
var btnID = 'btn12'; // 已知 button id
$('#' + btnID).text('cccc'); // 设置按钮文本为 'cccc'
alert($('#' + btnID).text()); // 显示按钮文本,应该弹出 'cccc'
});
</script>
</body>
</html>
这样么
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="jquery-1.11.0.min.js"></script>
</head>
<body>
<table>
<tr>
<td id="up1"><button id='btn11'>aaa</button></td>
<td id="up2"><button id='btn12'>bbb</button></td>
<tr>
</table>
<script>
$("table tr").each(function(){
var btnID='btn12';//已经button id
$('#' + btnID).text('cccc'); //设置按钮文本值
alert($('#' + btnID).text());//获取按钮文本值并弹出
})
</script>
</body>
</html>
代码如下:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="/jquery/jquery.js"></script>
</head>
<body>
<table>
<tr>
<td id="up1"><button id='btn11'>aaa</button></td>
<td id="up2"><button id='btn12'>bbb</button></td>
<tr>
</table>
<script>
var index=1;
$("table tr").each(function(){
var btnID='#btn1'+i;//已经button id
var temp=$(btnID).Text('cccc'); //赋值失败
alert($(btnID).Text());//无弹出数据
i++;
})
</script>
</body>
</html>