<th title="星期日" role="columnheader"><span>星</span></th>
<th title="星期一" role="columnheader"><span>星</span></th>
<th title="星期二" role="columnheader"><span>星</span></th>
<th title="星期三" role="columnheader"><span>星</span></th>
<th title="星期四" role="columnheader"><span>星</span></th>
<th title="星期五" role="columnheader"><span>星</span></th>
<th title="星期六" role="columnheader"><span>星</span></th>
如上html,如何用jquery将上面代码变成:
<th title="星期日" role="columnheader"><span>日</span></th>
<th title="星期一" role="columnheader"><span>一</span></th>
<th title="星期二" role="columnheader"><span>二</span></th>
<th title="星期三" role="columnheader"><span>三</span></th>
<th title="星期四" role="columnheader"><span>四</span></th>
<th title="星期五" role="columnheader"><span>五</span></th>
<th title="星期六" role="columnheader"><span>六</span></th>
<th title="星期一" role="columnheader"><span>星</span></th>
<th title="星期二" role="columnheader"><span>星</span></th>
<th title="星期三" role="columnheader"><span>星</span></th>
<th title="星期四" role="columnheader"><span>星</span></th>
<th title="星期五" role="columnheader"><span>星</span></th>
<th title="星期六" role="columnheader"><span>星</span></th>
如上html,如何用jquery将上面代码变成:
<th title="星期日" role="columnheader"><span>日</span></th>
<th title="星期一" role="columnheader"><span>一</span></th>
<th title="星期二" role="columnheader"><span>二</span></th>
<th title="星期三" role="columnheader"><span>三</span></th>
<th title="星期四" role="columnheader"><span>四</span></th>
<th title="星期五" role="columnheader"><span>五</span></th>
<th title="星期六" role="columnheader"><span>六</span></th>
<th title="星期日" role="columnheader"><span>星</span></th>
<th title="星期一" role="columnheader"><span>星</span></th>
<th title="星期二" role="columnheader"><span>星</span></th>
<th title="星期三" role="columnheader"><span>星</span></th>
<th title="星期四" role="columnheader"><span>星</span></th>
<th title="星期五" role="columnheader"><span>星</span></th>
<th title="星期六" role="columnheader"><span>星</span></th>
</table>
<script type="text/javascript">
$("th[role='columnheader']").each(function(i,item){
$(this).find("span").text($(this).attr("title").substring(2,3));
})
</script>
$('th').find('span').each(function(index, dom) {
$(this).html($(this).parent().attr('title').substring(2, 3));
});
});
<th title="星期日" role="columnheader"><span>星</span></th>
<th title="星期一" role="columnheader"><span>星</span></th>
<th title="星期二" role="columnheader"><span>星</span></th>
<th title="星期三" role="columnheader"><span>星</span></th>
<th title="星期四" role="columnheader"><span>星</span></th>
<th title="星期五" role="columnheader"><span>星</span></th>
<th title="星期六" role="columnheader"><span>星</span></th>
</table>
加上ID效率高点,多个的就用1,2L的方法 $('#t1 th span').each(function(i,v){
v.innerHTML=v.parentNode.title.slice(-1)
});
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/JavaScript">
$(function(){
$("#abv").click(function(){
var ar=new Array();
ar=["日","一","二","三","四","五","六"];
for(var i=0;i<ar.length;i++){
$("th").eq(i).empty().append(ar[i]);
}
});
})
</script><input type="button" id="abv" value="测试用按钮"/>
<table border="1" cellpadding="15" >
<tr>
<th title="星期日" role="columnheader"><span>星</span></th>
<th title="星期一" role="columnheader"><span>星</span></th>
<th title="星期二" role="columnheader"><span>星</span></th>
<th title="星期三" role="columnheader"><span>星</span></th>
<th title="星期四" role="columnheader"><span>星</span></th>
<th title="星期五" role="columnheader"><span>星</span></th>
<th title="星期六" role="columnheader"><span>星</span></th>
</tr>
</table>
lz这个其实只要更新<span>标签里的内容就可以了:
<table>
<th title="星期日" role="columnheader"><span>星</span></th>
<th title="星期一" role="columnheader"><span>星</span></th>
<th title="星期二" role="columnheader"><span>星</span></th>
<th title="星期三" role="columnheader"><span>星</span></th>
<th title="星期四" role="columnheader"><span>星</span></th>
<th title="星期五" role="columnheader"><span>星</span></th>
<th title="星期六" role="columnheader"><span>星</span></th>
</table>$(document).ready(function() {
$("th span").each(function(index, dom) {
$(this).text($(this).parent().attr('title').substring(2, 3));
});
});
<th title="星期日" role="columnheader"><span>星</span></th>
<th title="星期一" role="columnheader"><span>星</span></th>
<th title="星期二" role="columnheader"><span>星</span></th>
<th title="星期三" role="columnheader"><span>星</span></th>
<th title="星期四" role="columnheader"><span>星</span></th>
<th title="星期五" role="columnheader"><span>星</span></th>
<th title="星期六" role="columnheader"><span>星</span></th>
</table>
再简化一下$('#t1 th span').each(function(i,v){
v.innerHTML='日,一,二,三,四,五,六'.charAt(i)
});
v.innerHTML='日一二三四五六'.charAt(i)
});