通过组合 string & 来创建新的 JavaScript 变量多变的

发布于 2024-10-17 02:33:17 字数 395 浏览 1 评论 0原文

所以这是我的问题。

pageTitle1 = ('This is page one');
pageTitle2 = ('This is page two');
pageTitle3 = ('This is page three');

currentPageTitle = ('pageTitle'+currentPosition);

$("#pageNumber").html(currentPageTitle);

我试图根据我所在幻灯片的 currentPosition 在屏幕上显示当前页面标题。我遇到的问题是,当我需要显示“这是第一页”时,上面的代码在屏幕上显示“pageTitle1”。我对 javascript 很陌生,所以我感觉这是一个简单的解决方案,但几天的谷歌搜索并没有给我带来任何结果。

So this is my problem.

pageTitle1 = ('This is page one');
pageTitle2 = ('This is page two');
pageTitle3 = ('This is page three');

currentPageTitle = ('pageTitle'+currentPosition);

$("#pageNumber").html(currentPageTitle);

I'm trying to display the current page title to the screen based on the currentPosition of the slide I am on. The issue I'm having is the code above is displaying "pageTitle1" to the screen when I need it to display "This is page one". I'm newish to javascript so I have a feeling this an easy fix but days of googling has yielded me nothing.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一袭水袖舞倾城 2024-10-24 02:33:17

使用数组而不是单独命名每个变量。

var pageTitles = [
 'This is page one',
 'This is page two',
 'This is page three'
];

var currentPageTitle = pageTitles[currentPosition];

您的代码不起作用的原因是它只是添加两个字符串 "pageTitle"currentPosition 并将连接的字符串分配给 currentPageTitle。相反,您想要的是获取存储在具有该名称的变量中的值。假设您在全局范围内创建这些(顺便说一句,这不是一件好事),您可以这样做:

var currentPageTitle = window['pageTitle'+currentPosition];

但是,使用数组是更好的方法。

Use an array instead of naming each variable individually.

var pageTitles = [
 'This is page one',
 'This is page two',
 'This is page three'
];

var currentPageTitle = pageTitles[currentPosition];

Reason why your code is not working is because it is simply adding the two strings "pageTitle" and currentPosition and assigning the concatenated string to currentPageTitle. What you instead want is to get the value stored in the variable having that name. Assuming you are creating these in the global scope (which is not a good thing btw), you can do:

var currentPageTitle = window['pageTitle'+currentPosition];

However, going with arrays is a better approach.

夏末 2024-10-24 02:33:17

做这样的事情。

var pageTitle = new Array();
pageTitle.push('This is page one');
pageTitle.push('This is page two');
pageTitle.push('This is page three');

currentPageTitle = (pageTitle[currentPosition]);
$("#pageNumber").html(currentPageTitle);

或者简单地使用 eval (真的超级不推荐)

var currentPageTitle = eval('pageTitle'+currentPosition);

Do something like this.

var pageTitle = new Array();
pageTitle.push('This is page one');
pageTitle.push('This is page two');
pageTitle.push('This is page three');

currentPageTitle = (pageTitle[currentPosition]);
$("#pageNumber").html(currentPageTitle);

Or simply use eval (really super unrecommended)

var currentPageTitle = eval('pageTitle'+currentPosition);
三月梨花 2024-10-24 02:33:17

无需任何其他修改,您可以将此行替换

currentPageTitle = ('pageTitle'+currentPosition);

为:

currentPageTitle = window['pageTitle'+currentPosition];

Without any other modifications, you can replace this line

currentPageTitle = ('pageTitle'+currentPosition);

with this:

currentPageTitle = window['pageTitle'+currentPosition];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文