- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在制作一个书店网站(用于一个小型大学 javascript 项目)到目前为止,我所有的图书信息都位于 .js 文件中的数组中,如下所示..
var headings = new Array(4);
headings [0] = "title"
headings [1] = "author"
headings [2] = "cat"
headings [3] = "bookid"
headings [4] = "cost"
var bookid = new Array(6);
var title = new Array(6);
var cat = new Array(6);
var author = new Array(6);
var cost = new Array(6);
var desc = new Array(6);
bookid [0] = "0001"
title [0] = "For whom the tell bowls";
cat[0] = "fiction";
author[0] = "John Doe";
cost[0] = "€12.99";
desc[0] = "This book is frickin' amazing blah blah blah"
依此类推...共有7本书...
现在我想在单击缩略图时生成一个表格,其中一列中包含作者、标题、成本等,另一列中包含实际相应的详细信息,这是代码
for(var j = 0; j < 5; j++) {
row = document.createElement("tr");
// Each with 2 cells
for(var i = 0; i < 2; i++) {
var cell = document.createElement("td");
var temp = headings[j];
var temp2 = (temp + '[' + filename + ']');
if (i==1)
{var text = document.createTextNode(temp2);
}
else
{var text = document.createTextNode(temp);}
顺便说一句,“文件名”被传递给这个函数,它是这本书的#,例如,0、1、2等等......现在,页面上显示的内容是正确的.. 对于第 0 本书,我得到了..
title title[0]
author author[0]
cat cat[0]
bookid bookid[0]
cost cost[0]
对于第一本书,我得到了
title title[1]
author author[1]
cat cat[1]
bookid bookid[1]
cost cost[1]
等等,但详细信息不会被解释为数组中的实际变量。我猜测该解决方案与“innerHTML”有关,或者可能以某种方式转换我的变量..
如果有人知道这个问题的解决方案,我们将不胜感激!...我计划今天完成这个网站,然后转向其他更重要的事情(因为这个项目不值得很多分)但是留下这个缺陷在那里会打扰我!
最佳答案
您现在解决这个问题的方法是(尝试)通过名称访问变量,例如您想使用 "title"
查找名为 title
的变量。 不要这样做。您最终会污染全局范围并依赖 window
来访问这些变量,或者您将落入 >评估
魔鬼。
这里有一个更好的主意:制裁剪体来存放你的书。一本书将如下所示:
var book = {
title: "Foo",
author: "Bar",
cat: "Baz",
bookid: 123456,
cost: "One hundred billion dollars"
};
更好的是定义一种书籍类型:
function Book(title, author, cat, bookid, cost) {
this.title = title;
this.author = author;
this.cat = cat;
this.bookid = bookid;
this.cost = cost;
}
var book = new Book("Foo", "Bar", "Baz", 123456, "One hundred billion dollars");
您可以将这些书籍存储到一个书籍数组中:
var books = [book1, book2, book3, ...];
突然之间,制作表格变得很容易!
// Create or get a table
var table = document.createElement("table");
// Loop over the books array
for (var i=0, l=books.length; i < l; ++i) {
// Get the current book
var book = books[i];
// Create a table row
var row = document.createElement("tr");
// Loop over the *properties* of the book
for (var propertyName in book) {
// Skip inherited properties
if(!book.hasOwnProperty(propertyName)) continue;
// Get the *property value*
var property = book[propertyName];
// Create a table cell and append to the row
var textNode = document.createTextNode(property);
var cell = document.createElement("td");
cell.appendChild(textNode);
row.appendChild(cell);
}
// Append the row to the table
table.appendChild(row);
}
您甚至可以使用一本书元素创建表格标题:
// Some book to loop over its property names
var someBook = books[0];
var row = document.createElement("tr");
// Loop over the book properties
for(var propertyName in someBook) {
// Now placing the *property name* in a text node
var textNode = document.createTextNode(propertyName);
var header = document.createElement("th");
cell.appendChild(textNode);
row.appendChild(header);
}
table.appendChild(row);
正如丹尼斯所建议的,您可能希望对标题有更多的控制。您不想将 "cat"
显示为图书类别的标题,您想要更具可读性的内容。另外,您可能不想显示存储在书籍对象上的所有属性。在这种情况下,您可以存储要在另一个对象中显示的标题,并添加有关这些标题的更多信息:
var headings = {
title: {
text: "Book title"
},
author: {
text: "Author"
},
cat: {
text: "Category"
},
bookid: {
text: "ID"
},
cost: {
text: "Price"
}
};
在数据循环中,您将迭代 headings
而不是 book
的属性。
// Loop over the properties to display
for (var propertyName in headings) {
// Get the property value
var property = book[propertyName];
// [snipped]
}
在标题循环中,您可以使用 headings[propertyName].text
作为显示名称,而不仅仅是 propertyName
。您不再需要图书实例,因此可以删除 someBook
变量。
// Loop over the book properties
for(var propertyName in headings) {
var headingName = headings[propertyName].text;
var textNode = document.createTextNode(headingName);
// [snipped]
}
关于JavaScript、表和数组。棘手的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10871900/
我有一台 MySQL 服务器和一台 PostgreSQL 服务器。 需要从多个表中复制或重新插入一组数据 MySQL 流式传输/同步到 PostgreSQL 表。 这种复制可以基于时间(Sync)或事
如果两个表的 id 彼此相等,我尝试从一个表中获取数据。这是我使用的代码: SELECT id_to , email_to , name_to , status_to
我有一个 Excel 工作表。顶行对应于列名称,而连续的行每行代表一个条目。 如何将此 Excel 工作表转换为 SQL 表? 我使用的是 SQL Server 2005。 最佳答案 这取决于您使用哪
我想合并两个 Django 模型并创建一个模型。让我们假设我有第一个表表 A,其中包含一些列和数据。 Table A -------------- col1 col2 col3 col
我有两个表:table1,table2,如下所示 table1: id name 1 tamil 2 english 3 maths 4 science table2: p
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 1 年前。 Improve th
下面两个语句有什么区别? newTable = orginalTable 或 newTable.data(originalTable) 我怀疑 .data() 方法具有性能优势,因为它在标准 AX 中
我有一个表,我没有在其中显式定义主键,它并不是真正需要的功能......但是一位同事建议我添加一个列作为唯一主键以随着数据库的增长提高性能...... 谁能解释一下这是如何提高性能的? 没有使用索引(
如何将表“产品”中的产品记录与其不同表“图像”中的图像相关联? 我正在对产品 ID 使用自动增量。 我觉得不可能进行关联,因为产品 ID 是自动递增的,因此在插入期间不可用! 如何插入新产品,获取产品
我有一个 sql 表,其中包含关键字和出现次数,如下所示(尽管出现次数并不重要): ____________ dog | 3 | ____________ rat | 7 | ____
是否可以使用目标表中的LAST_INSERT_ID更新源表? INSERT INTO `target` SELECT `a`, `b` FROM `source` 目标表有一个自动增量键id,我想将其
我正在重建一个搜索查询,因为它在“我看到的”中变得多余,我想知道什么 (albums_artists, artists) ( ) does in join? is it for boosting pe
以下是我使用 mysqldump 备份数据库的开关: /usr/bin/mysqldump -u **** --password=**** --single-transaction --databas
我试图获取 MySQL 表中的所有行并将它们放入 HTML 表中: Exam ID Status Assigned Examiner
如何查询名为 photos 的表中的所有记录,并知道当前用户使用单个查询将哪些结果照片添加为书签? 这是我的表格: -- -- Table structure for table `photos` -
我的网站都在 InnoDB 表上运行,目前为止运行良好。现在我想知道在我的网站上实时发生了什么,所以我将每个页面浏览量(页面、引荐来源网址、IP、主机名等)存储在 InnoDB 表中。每秒大约有 10
我在想我会为 mysql 准备两个表。一个用于存储登录信息,另一个用于存储送货地址。这是传统方式还是所有内容都存储在一张表中? 对于两个表...有没有办法自动将表 A 的列复制到表 B,以便我可以引用
我不是程序员,我从这个表格中阅读了很多关于如何解决我的问题的内容,但我的搜索效果不好 我有两张 table 表 1:成员 id*| name | surname -------------------
我知道如何在 ASP.NET 中显示真实表,例如 public ActionResult Index() { var s = db.StaffInfoDBSet.ToList(); r
我正在尝试运行以下查询: "insert into visits set source = 'http://google.com' and country = 'en' and ref = '1234
我是一名优秀的程序员,十分优秀!