发表于: 2016-06-29 23:58:49
1 2247
今天完成的事情:用jquery重写task1-4
明天计划的事情:参考别人的代码重构task4,用jquery写task5
遇到的问题:。。。我好想写项目啊啊啊啊
var boxcolor = $(".box:eq(2)").attr("style");
console.log(boxcolor);
boxcolor = "background: yellow";
console.log(boxcolor);
console.log(boxcolor);
boxcolor = "background: yellow";
console.log(boxcolor);
这么写虽然改变了容器里的值然而并没有效果
。。。。。
死活不是黄色
问题2:
这么写是对的,难道jquery对象可以使用原生的属性嘛
$box[i].style = "background:blue";
这么这样写就是错的,不是函数??
$box[i].css("background","blue");
然而这么写也是ok
$(".box:eq("+i+")").css("background","red");
写(“+i+”)的原因:
首先$() 括号里面的是字符串 ,因为它是用引号括起来的
然后举个类似的例子让你参考下
alert()这个记得把,如果里面是字符串,那么也是必须加引号的,如果里面是变量呢?那就不需要加
例如var i = 1;
alert(i); 结果是1 但是如果alert("i"),那么这个输出就是 i 了
alert("li:eq()"),这个出来的就是 // li:eq() 这个结果吧
现在我要把变量值夹在括号之间怎么写呢??相信你已经知道了吧
区别就在于 alert("li:eq("+i+")") 输出 li:eq(变量i的值) 而alert("li:eq(i)") 输出是 li:eq(i)
亮代码~~~
var i = 0;
function myfunction() {
var number = 0;
var colortext = new Array("红色","绿色","蓝色");
// var box = document.getElementsByClassName("box");
var $box = $(".box");
$box.css("background","aqua");
i = parseInt(Math.floor(Math.random() * 9));
number = Math.floor(Math.random() * 3);
switch (number) {
case 0:
// box[i].style.background="red";
$(".box:eq("+i+")").css("background","red");
break;
case 1:
// box[i].style.background="green";
$box[i].style = "background:green";
break;
case 2:
// box[i].style.background="blue";
$(".box:eq("+i+")").attr("style","background:blue");
break;
}
console.log($box[i]);
console.log("格子" + (i + 1) + "变成了" + colortext[number]);
}
setInterval("myfunction()", 1000);
function myfunction() {
var number = 0;
var colortext = new Array("红色","绿色","蓝色");
// var box = document.getElementsByClassName("box");
var $box = $(".box");
$box.css("background","aqua");
i = parseInt(Math.floor(Math.random() * 9));
number = Math.floor(Math.random() * 3);
switch (number) {
case 0:
// box[i].style.background="red";
$(".box:eq("+i+")").css("background","red");
break;
case 1:
// box[i].style.background="green";
$box[i].style = "background:green";
break;
case 2:
// box[i].style.background="blue";
$(".box:eq("+i+")").attr("style","background:blue");
break;
}
console.log($box[i]);
console.log("格子" + (i + 1) + "变成了" + colortext[number]);
}
setInterval("myfunction()", 1000);
收获:
task4
$text = $(".mainMiddle:eq(0)");
console.log($text);
var text = "";
for (var i=1;i <= player.length;i++){
text += '<div class="box">' + '<div class="person">' + player[i-1] + '</div>' + '<div class="number">' + i + '</div>' + '</div>'
}
$text.html(text);
console.log($text);
var text = "";
for (var i=1;i <= player.length;i++){
text += '<div class="box">' + '<div class="person">' + player[i-1] + '</div>' + '<div class="number">' + i + '</div>' + '</div>'
}
$text.html(text);
由于html()的返回值是string,所以直接吧生成好的string放在html()中,其他没什么好说的。。
jquery我觉得就是查找和dom方便一点。。后面再看吧
评论