工作之余,想起来最开始学习编程的时候,写的九九乘法表,突然兴起,写了一份,记录了下载
js版本:
//html代码
<div id="chengfa">
</div>
//js代码
<script type="text/javascript">
$(document).ready(function(){
var num = 9;
var string
for(var i = 1; i <= num; i++){
for(var j = 1; j <= i; j++){
var jieguo = i * j;
string += "<td style='border:1px solid #000000;'>"+j+ "X" +i+"="+jieguo+ " ";
}
string ="<tr>"+ string + "</tr>";
}
document.getElementById("chengfa").innerHTML = "<table>" + string +"</table>";
});
</script>
效果图
PHP版本:
public function chengfa()
{
$num = 9;
$i = 1;
$j = 1;
for ($i=1; $i < $num; $i++) {
for ($j=1; $j < $i; $j++) {
$jieguo = $i * $j;
$string .= "<td style='border:1px solid #000000;'>".$j. "X" .$i."=".$jieguo. " ";
}
$string = "<tr>". $string . "<tr>";
}
$string1 = "<table>" . $string ."</table>";
echo $string1;
}
效果图
本质都是for循环遍历,然后包装上td和tr标签