日期:2014-05-16  浏览次数:20323 次

跪求这几题答案,会追加分的,谢谢!万分感谢!
1、使用javascript实现,将文档中className有“test”的td标签背景色设为黄色。
2、用javascript实现控制一个文本框的输入字数限制,超出字数限制文本框飘红显示。
3、使用Javascript打印出1-10000之间的所有素数。
谢谢!

------解决方案--------------------
别跪了,站起来吧 给自己留点尊严呗

JScript code


var result = [2], i = 3, sqrtVal, flag;
            while(i < 10000) {
                sqrtVal = Math.floor(Math.sqrt(i));
                flag = false;
                for (var stack in result) {
                    if(result[stack] <= sqrtVal && i % result[stack] == 0) {
                        flag = true;
                        break;
                    }
                }
                if(!flag) {
                    result.push(i);
                }
                i += 2;
            }
            console.log(result);

------解决方案--------------------
不要见人就跪!

直接贴出代码:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
 <head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>百度知道测试</title>
<style type = "text/css">
#result{
width:100%;
max-height = 100px;
overflow-y:auto;
border:1px solid #ccc;
}
</style>
<script type = "text/javascript" src = "../js/jquery-1.7.1.js"></script>
<script type = "text/javascript">
$(function(){
getAllSUSHU();
$(document).click(function(){
$("td").each(function(){
var className = $(this).attr("class");
if(className && className.indexOf("test") != -1){
$(this).css("background","yellow");
}
})
})

$("#ipt").keyup(function(){
if($(this).val().length>10){
$(this).css("border-color","red");
}else{
$(this).css("border-color","");
}
})

function getAllSUSHU(){ //获取1-10000之间的所有素数(思想:判断一个整数m是否是素数,只需把m被2~m-1之间的每一个整数去除,如果都不能被整除,那么m就是一个素数)
var n = 10000;
var sushu = []; //用于存放筛选出的素数
var r = "";
for(var i = 1;i<=n;i++){
var t = 0; //第一次外层循环,都设置一个标识位。
for(var j = 2;j<i;j++){
if(i%j == 0){
t++;
}
}
if(t==0){
sushu.push(i);
}
}
for(var k = 0;k<sushu.length;k++){
r += sushu[k]+" , ";
}
$("#result").html(r);
}
})
</script>
 </head>
 <body>
<div id = "main">
<p>
第一题测试方法:在文档空白处点击。所有className属性值里中包含test的单元格背景将变为黄色;
</p>
<p>
第二题测试方法:直接在文本框里输入字符,超过10个时,文本框边框将变为红色,少于10时,边框为默认的黑色;
</p>
<table>
<tr>
<td class = 'testTt'>1</td><td>2</td><td>3</td>
</tr>
<tr>
<td>1</td><td class = "rr">2</td><td class = "testOk">3</td>
</tr>
<tr>
<td>1</td><td class = "YYtest">2</td><td>3</td>
</tr>
</table>
<input type = "text" id = "ipt"/>
<div id = "result">

</div>
</div>
 </body>
</html>