[英]How can I use a numeric variable in object name when access the object's properties
var showcaseIndex = 1; var showcaseItem1 = { projectTitle: "Adrian's Caterers", projectRole: "Web Designer & Developer <br />", rojectClient: "Catering Business Owner", projectDescription: "" }; var showcaseItem2 = { projectTitle: "Inspiring", projectRole: "Web Designer & Developer", projectClient: "Medical Trasnport Start-up", projectDescription: "" }; $('.showcase-content h1').html(showcaseItem[showcaseIndex].projectTitle); $('.showcase-content #role').html(showcaseItem[showcaseIndex].projectRole);
.showcase-content { width: 500px; height: 70vh; background: rgba(255,255,255,1); float: left; box-sizing: border-box; padding: 20px; } .showcase-content h1, h2, p { color: #4e4e4e; text-align: left; margin: 0; font-family: 'quicksandregular', sans-serif; margin-bottom: 10px; }
<div class="showcase-content"> <h1></h1> <h2 id="role">Role in Project <br /> Client</h2> <h2>Description</h2> <p> Some Content inserted with PHP</p> </div>
Hi guys, kinda off my coding game here so I need some help. 大家好,我的编码游戏还不错,所以我需要一些帮助。 I'm building my portfolio website and the page in which will display my past projects will be full screen. 我正在建立我的投资组合网站,将显示我过去的项目的页面将变为全屏。 I've stored information in objects with the same name with the exception of the number at the end. 我将信息存储在同名对象中,但末尾的数字除外。 There is also an variable index, my goal was change the content of my portfolio page by clicking on an arrow which will start a function to change the content, but I can't seem to get pass making the content show up period. 还有一个变量索引,我的目标是通过单击箭头来更改我的投资组合页面的内容,这将启动一个功能来更改内容,但是我似乎无法通过使该内容显示期间。 I hope I'm making sense here, if not I will be happy to clarify further. 我希望我在这里很有道理,如果没有的话,我很乐意进一步澄清。
How can I use a variable(numeric) as apart of an object name to access that objects properties. 如何使用变量(数字)作为对象名称的一部分来访问该对象的属性。
According to the Zach-M and ASDFGerte suggestions, use an Array instead an object and get your profile settings with an index. 根据Zach-M和ASDFGerte的建议,使用Array代替对象,并使用索引获取配置文件设置。
In this solution, every property is written in the doc, not just two. 在此解决方案中,每个属性都写在文档中,而不仅仅是两个。
Notice the correspondence between the ID properties in the HTML tag and the property names in the profiles. 注意HTML标记中的ID属性与配置文件中的属性名称之间的对应关系。 It's for readability and convenience. 这是为了便于阅读和方便。
(function () { "use strict"; $(document).ready(function () { var showCaseIndex = 1; // so the second one let showcase = [ { projectTitle: "Adrian's Caterers" , projectRole: "Web Designer & Developer" , projectClient: "Catering Business Owner" , projectDescription: "" }, { projectTitle: "Inspiring" , projectRole: "Web Designer & Developer" , projectClient: "Medical Transport Start-up" // <- typo here "trasnport" , projectDescription: "" } ] for(var prop in showcase[showCaseIndex]) { $(".showcase-content #"+prop).html(showcase[showCaseIndex][prop]); } }); }());
.showcase-content { width: 500px; height: 70vh; background: rgba(255,255,255,1); float: left; box-sizing: border-box; padding: 20px; } .showcase-content h1, h2, p { color: #4e4e4e; text-align: left; margin: 0; font-family: 'quicksandregular', sans-serif; margin-bottom: 10px; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="showcase-content"> <h1 id="projectTitle"></h1> <h2 id="projectRole">Role in Project <br /> Client</h2> <p id="projectClient"></p> <h2 id="projectDescription">Description</h2> <p id="projectDescription"></p> <p> Some Content inserted with PHP</p> </div>
You could use eval
to evaluate your object's name and access it that way, but doing so is seriously NOT recommended . 您可以使用eval
评估对象的名称并以这种方式访问它,但绝对不建议这样做。
As mentioned by Zach M. and ASDFGerte , using an Array
is definitely the way to go here. 正如Zach M.和ASDFGerte所述 ,使用Array
绝对是解决问题的方法。
Functional example: 功能示例:
(function () { "use strict"; $(document).ready(function () { var showcase = []; var showcaseIndex = 1; showcase.push({ projectTitle: "Adrian's Caterers", projectRole: "Web Designer & Developer <br />", // <-- <br /> here seems out of place... rojectClient: "Catering Business Owner", projectDescription: "" }); showcase.push({ projectTitle: "Inspiring", projectRole: "Web Designer & Developer", projectClient: "Medical Trasnport Start-up", projectDescription: "" }); $('.showcase-content h1').html(showcase[showcaseIndex].projectTitle); $('.showcase-content #role').html(showcase[showcaseIndex].projectRole); }); }());
.showcase-content { width: 500px; height: 70vh; background: rgba(255,255,255,1); float: left; box-sizing: border-box; padding: 20px; } .showcase-content h1, h2, p { color: #4e4e4e; text-align: left; margin: 0; font-family: 'quicksandregular', sans-serif; margin-bottom: 10px; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="showcase-content"> <h1></h1> <h2 id="role">Role in Project <br /> Client</h2> <h2>Description</h2> <p> Some Content inserted with PHP</p> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.