如何在 JavaScript 函数中设置新的变量值?

发布于 2024-10-10 05:54:21 字数 779 浏览 0 评论 0原文

我有 2 个导航按钮“a”和“b”,其中“a”链接到页面“A”,“b”链接到页面“B”。页面加载时,页面“A”默认可见,页面“B”默认隐藏。单击链接“b”时,页面“A”将淡出至不透明度 0,并通过 jQuery“toggle”方法减小高度,而页面“B”将淡入至不透明度 1,并通过jQuery“切换”方法。当再次单击链接“a”时,会发生相反的情况,页面“b”再次从视图中隐藏,并且使用相同的方法将页面“a”带回视图。

我遇到的问题是,当再次单击当前显示页面的链接时,当前页面会变成空白;如果页面“A”已加载,并且我单击链接“a”,则整个页面变为空白,这是我不希望的。我尝试过以下内容:

var i = "a";

function a() {
  if (i != "a") {
    var i = "a";    
    jQuery(animation to hide page 'A');
    jQuery(animation to make visible page 'B');
  }
}

function b() {
  if (i != "b") {
    var i = "b";
    jQuery(animation to hide page 'B');
    jQuery(animation to make visible page 'B');
  }
}

我发现我在函数内部设置的变量“i”的值仅在函数内部有效,而在函数外部,变量的值保持为“a”,因此链接“ a' 永远不可点击,而链接 'b' 始终可点击,并且还会导致空白页面。

我怎样才能编写正确的 JavaScript 来完成我想要它做的事情?

I have 2 navigation buttons, 'a' and 'b', where 'a' links to page 'A' and 'b' links to page 'B'. When the page loads, page 'A' is visible by default and page 'B' is hidden by default. When link 'b' is clicked, page 'A' is faded out to opacity 0 and reduced in height via the jQuery 'toggle' method, while page 'B' is faded in to opacity 1 and increased in height to full size via the jQuery 'toggle' method. When link 'a' is clicked again, the opposite happens where page 'b' is once again hidden from view and page 'a' brought back to view using the same methods.

What I'm having trouble with is when the link to the current page being displayed is clicked again, the current page just goes blank; if page 'A' is already loaded, and I click on link 'a', the whole page goes blank, which I don't want. What I have tried is the following:

var i = "a";

function a() {
  if (i != "a") {
    var i = "a";    
    jQuery(animation to hide page 'A');
    jQuery(animation to make visible page 'B');
  }
}

function b() {
  if (i != "b") {
    var i = "b";
    jQuery(animation to hide page 'B');
    jQuery(animation to make visible page 'B');
  }
}

I have figured out that the value for variable 'i' that I set inside a function is only effective inside the function and that outside the function, the variable's value remains as 'a' and hence link 'a' is never clickable while link 'b' is always clickable and also results in a blank page.

How can I write the correct javascript to do what I want it to do?

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

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

发布评论

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

评论(2

少女情怀诗 2024-10-17 05:54:21

var 关键字放入两个函数中,它将修改全局 i 变量。

您的代码实际上非常具有误导性,因为您将 i 分配给 function,您可能只想将其更改为使用字符串,如下所示:

var i = "a";

function a() {
  if (i != "a") {
    i = "a";    
    jQuery(animation to hide page 'A');
    jQuery(animation to make visible page 'B');
  }
}

function b() {
  if (i != "b") {
    i = "b";
    jQuery(animation to hide page 'B');
    jQuery(animation to make visible page 'B');
  }
}

Drop the var keyword inside both functions and it will modify the global i variable.

Your code is actually very misleading because you're assigning i to a function, you might want to just change it to use strings, like so:

var i = "a";

function a() {
  if (i != "a") {
    i = "a";    
    jQuery(animation to hide page 'A');
    jQuery(animation to make visible page 'B');
  }
}

function b() {
  if (i != "b") {
    i = "b";
    jQuery(animation to hide page 'B');
    jQuery(animation to make visible page 'B');
  }
}
呆° 2024-10-17 05:54:21

您在函数中再次定义了一个新变量 i,因为您在 i 之前使用了 var。
将它们更改为如下:

var i = a;

function a() {
  if (i != a) {
    i = a;    
    jQuery(animation to hide page 'A');
    jQuery(animation to make visible page 'B');
  }
}

function b() {
  if (i != b) {
    i = b;
    jQuery(animation to hide page 'B');
    jQuery(animation to make visible page 'B');
  }
}

You are defining a new variable i again in the function because you are using var before i.
Change them to as follows:

var i = a;

function a() {
  if (i != a) {
    i = a;    
    jQuery(animation to hide page 'A');
    jQuery(animation to make visible page 'B');
  }
}

function b() {
  if (i != b) {
    i = b;
    jQuery(animation to hide page 'B');
    jQuery(animation to make visible page 'B');
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文