JavaScript的。根据用户输入创建动态表

时间:2017-07-07 22:07:00

标签: javascript

我尝试通过提示输入一个大小为0的整数,并且是数字。我做了一个while while循环,似乎工作正常。有了这个数字,我必须将它传递给名为" genTable"并根据用户键入的内容创建一个包含动态行数的表。但是,使用我当前的代码,我似乎无法让表格显示在页面上。关于我哪里出错以及如何解决它的任何想法?

<!DOCTYPE HTML>

<html>
<head>
<title>jsLoopDemo</title>

<!-- 
Honor Code: I acknowledge that this code represents my own work: CC
Date:  July 6, 2017
-->

<meta charset="utf-8" />
<meta name="description" content="Create a chart with rows based on a number 
user chose." />
<meta name="keywords" content="loop, row, dynamic" />
<meta name="author" content="" />


<script type="text/javascript">


    do{
       var numChose = prompt("Please enter an interger greater than zero.");
    }while (isNaN(numChose) || numChose % 1 !== 0 || numChose < 1 );

    function genTable(numChose)
    {
        var table = document.createElement("TABLE");
        var tableBody = document.createElement("TBODY");
        table.appendChild(tableBody);
        var myTableDiv = document.getElementById("mytable");


        for (var r = 0; r <= numChose; r++)
            {
                var tr = document.createElement("TR");
                tableBody.appendChild(tr);
                var td = document.createElement("TD");
                tr.appendChild(td);
                td.appendChild(document.createTextNode("Row " + r));
            }
     myTableDiv.appendChild(table);
    }



</script>



</head>

<body> 

<div id="mytable">   
</div>

</body>
</html>

2 个答案:

答案 0 :(得分:0)

两件事:

您需要在提示后实际调用该函数,因此您需要在执行后添加一行:

class CollectionViewLayout: UICollectionViewLayout {
     //Invalidate the layout every time the bounds change so we can do the cool changes
    override func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) -> Bool {
        return true
    }

    override var collectionViewContentSize: CGSize {
        return CGSize(width:  self.itemSize * CGFloat(COLS),
                  height: self.itemSize * CGFloat(ROWS))
    }

    override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
        var attributes: [UICollectionViewLayoutAttributes] = []
        for i in 0..<cellCount {
            let indexPath = IndexPath(item: i, section: 0)
            attributes.append(self.layoutAttributesForItem(at: indexPath)!)
        }

        return attributes
    }

    override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
        var attributes = UICollectionViewLayoutAttributes(forCellWith: indexPath)

        // **Code where I create "z" variable by using equation in article, where "z" is just a value [0,1] that determines cell's scale**

        // **More code where I Calculate "x" and "y" based on indexPath like in article**    

        //Set the cell's attributes:
        attributes.transform = CGAffineTransform(scaleX: z, y: z)
        attributes.size = CGSize(width: self.itemSize, height: self.itemSize)
        attributes.center    = CGPoint(x: x, y: y)

        return attributes
    }

}

由于DOM未准备就绪(genTable(numChose); 未定义),因此仍然无法正常工作,因此您需要将整个代码包装在事件监听器中:

myTableDiv

答案 1 :(得分:0)

之后移动<div id="mytable"> </div> 代码
<body>
genTable(numChose);

,以便在生成表时在DOM上呈现元素并调用函数

{{1}}
从用户那里得到号码后