试图创建一个可重复使用的函数来生成一行中的单元格.(棋盘)
Trying to make a reusable function to make cells in a row. (chess board)
我正在尝试制作棋盘的第一排。我有它,但我正在努力制作我认为所谓的构造函数,以学习更高级的编程。我试图获得函数中的所有数据,并将div附加到板上——x应该通过乘以i
*工件大小来更新。我想我在使用这个新的关键词和把它附加在一起时遇到了问题。如果你能告诉我如何填满整个棋盘,那就太好了。我假设您将使用嵌套for循环。这是我的下一个目标。
我有这样的东西。
$(function(){
var boardHeight = parseInt($(".board").css("height")),
boardWidth = parseInt($(".board").css("width")),
amountOfPieces =8,
pieceSize = boardHeight / amountOfPieces,
board = $(".board");
console.log(pieceSize);
function Cell(orange, x){
this.width = pieceSize;
this.height = pieceSize;
this.color = orange ? "orange" : "black"
this.x = x;
}
console.log( new Cell())
for(var i = 0 ; i < amountOfPieces; i++){
if(i % 2 == 0){
board.append($("<div>") new cell("orange", i * pieceSize))
}else if( i % 2 == 1){
board.append($("<div>").css({
position: "absolute",
width : pieceSize,
height: pieceSize,
"background-color" : "black",
left: i * pieceSize
}))
}
}
});
编辑:好的,伙计们,我得到了答案中显示的第一行。现在我需要填写整个表格。记住颜色需要交替,我更喜欢使用嵌套的for循环。谢谢
您应该将pieceSize作为参数传入,如:
function Cell(orange, x, size) {
this.width = size;
this.height = size;
this.color = orange ? "orange" : "black"
this.x = x; // What is x used for anyway?
}
然后当你使用它时,它会看起来像(只是一个例子):
var createdCell = new Cell(true, 0, pieceSize);
此外,以下行在多个级别上都被搞砸了:
board.append($("<div>") new cell("orange", i * pieceSize))
查看jQuery .append()
文档,以更好地了解如何使用它:http://api.jquery.com/append/
你的Cell
构造函数所做的就是用游戏板上一个空格的参数创建对象。它实际上并没有构建它。您需要创建一个函数来接收new Cell()
生成的对象,并实际创建一个HTML字符串以附加到页面上。类似于:
function create(cell) {
var str = '<div class="whatever" style="';
str+= 'height: ' + cell.height + ';';
str+= 'width: ' + cell.width + ';';
str+= 'background: ' + cell.color + ';';
'">';
str += '</div>';
return str;
}
然后你可以做一些类似的事情:
board.append(create(new Cell(true, 0, pieceSize)))
这个答案不适合你复制/粘贴到你的项目中。这些示例为您提供了解决此问题所需的工具。
好的,我得出了这个答案。你觉得怎么样?
$(function(){
var boardHeight = parseInt($(".board").css("height")),
boardWidth = parseInt($(".board").css("width")),
amountOfPieces =8,
pieceSize = boardHeight / amountOfPieces,
board = $(".board");
console.log(pieceSize);
function Cell(orange, x){
this.width = pieceSize;
this.height = pieceSize;
this.background = orange ? "orange" : "black"
this.left = x;
this.position = "absolute"
}
console.log( new Cell())
for(var i = 0 ; i < amountOfPieces; i++){
if(i % 2 == 0){
var obj = new Cell("orange", i * pieceSize)
console.log(obj)
board.append($("<div>").css(obj))
}else if( i % 2 == 1){
var obj = new Cell("",i * pieceSize )
board.append($("<div>").css(obj))
}
}
})
.board{
background: #ccc;
width: 500px;
height: 500px;
margin: 20px auto;
position: relative;
}
.cell{
position: absolute;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
<div class="board"></div>
现在开始制作柱子。对那些家伙有什么帮助吗?
编辑:我想出了一个答案来填满整个黑板,它在下面。我仍然希望看到其他人的结果
$(function(){
var boardHeight = parseInt($(".board").css("height")),
boardWidth = parseInt($(".board").css("width")),
amountOfPieces =8,
pieceSize = boardHeight / amountOfPieces,
board = $(".board");
console.log(pieceSize);
function Cell(orange, x, y){
this.width = pieceSize;
this.height = pieceSize;
this.background = orange ? "orange" : "black"
this.left = x;
this.position = "absolute"
this.top = y
}
console.log( new Cell())
for(var i = 0; i < amountOfPieces ; i ++){
for(var j = 0 ; j < amountOfPieces; j++){
if(i % 2 == 0){
if(j % 2 == 1){
var obj = new Cell("orange", i * pieceSize , j * pieceSize)
console.log(obj)
board.append($("<div>").css(obj))
}else{
var obj = new Cell("", i * pieceSize , j * pieceSize)
console.log(obj)
board.append($("<div>").css(obj))
}
}else if( i % 2 == 1){
if(j % 2 == 1){
var obj = new Cell("", i * pieceSize , j * pieceSize)
board.append($("<div>").css(obj))
}else{
var obj = new Cell("orange", i * pieceSize , j * pieceSize)
board.append($("<div>").css(obj))
}
}
}
}
})
.board{
background: #ccc;
width: 500px;
height: 500px;
margin: 20px auto;
position: relative;
}
.cell{
position: absolute;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
<div class="board"></div>
尝试使用.slice()
、.filter()
、:odd
、:even
、.addBack()
、Array.prototype.reverse()
for (var i = n = 0, colors = ["orange", "black"]; i < 64; i++, n = i % 8 + 1) {
$("<div class=row>").appendTo("section");
if (n === 8) {
$(".row").slice(i === n - 1 ? 0 : $(".row").length - n, i + 1)
.filter(":even").css("background", colors[0])
.addBack().filter(":odd").css("background", colors[1]);
colors.reverse(); $("section").append("<br>");
}
}
div {
position: relative;
width: 65px;
height: 65px;
display: inline-block;
padding: 0;
margin-left: 2px;
margin-right: 2px;
outline: 2px solid brown;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">
</script>
<section>
</section>
相关文章:
- 在终端中运行 JavaScript 时(使用 rhino),如何使用 print() 函数在一行中打印
- 在追加一行并调用其中的时差函数时生成动态id
- JavaScript在一行中设置了命名函数的原型
- 怪异的JS行为:执行函数需要额外的一行
- 如果最后一行已经显示,如何防止jQuery加载更多停止函数
- 递归可防止函数在一行中被多次单击
- 什么'It’对一行代码使用匿名函数的意义何在
- 我正在编写一个 Javascript 函数,以便将表中的每一行着色为不同的颜色.为什么我的代码不起作用
- 如何在 Node.js 中为非常大 (> 1GB) 文件的每一行运行异步函数
- 如何让 javascript 保持,直到函数返回一个值,然后继续下一行
- 我如何创建一个函数,以便编程行将等到它完成并移动到 javascript 中的下一行
- jQuery回调函数在循环之前触发最后一行
- Javascript - 异步函数完成后下一行如何等待执行
- 如何向 Javascript 表添加一行,其中包含包含 jQuery 中的日期选择器函数的单元格
- 在一行中编写事件处理程序函数
- 可以't将两个函数的输出放在一行中
- GoogleSheets:如何使自定义函数的返回值溢出到一行中
- 试图创建一个可重复使用的函数来生成一行中的单元格.(棋盘)
- 在一行中使用多个函数进行Undercore
- Javascript时间戳函数一行