【问题标题】:Passing parameters to an inline javascript function将参数传递给内联 javascript 函数
【发布时间】:2017-02-21 13:26:44
【问题描述】:

我正在尝试通过循环为主页上的每个按钮分配一个功能。 当单击前三个按钮中的每一个时,它应该将会话存储中的一个项目设置为 true(草、软体动物或毛衣)。单击底部按钮时,应显示所有其他被单击的按钮以及植物一词。

例如 如果我单击“向我发送有关草的更多信息” 然后点击“您想了解更多信息的事情” 最后一个按钮应显示“草本植物” 如果我然后单击“向我发送有关软体动物的更多信息” 最后一个按钮应该显示“软体动物草植物”

我的 html 和 javascript 文件 (我已经指出我相当肯定的部分不能正常工作) (y[count].id 应该是“草”、“软体动物”或“毛衣”这个词) (infoChosen 是页面上的第四个也是最后一个按钮)

window.onload = function() {

  var y = document.getElementsByClassName("button");
  for (count = 0; count < y.length; count++) {
    if (y[count].id == "infoChosen") {
      y[count].onclick = function() {
        document.getElementById("infoChosen").innerHTML = "";
        if (sessionStorage["Molluscs"] == "true") {
          document.getElementById("infoChosen").innerHTML += "Green Molluscs\n";
        }
        if (sessionStorage["Sweaters"] == "true") {
          document.getElementById("infoChosen").innerHTML += "Green Sweaters\n";
        }
        if (sessionStorage["Grass"] == "true") {
          document.getElementById("infoChosen").innerHTML += "Grass\n";
        }
        if (sessionStorage["Plants"] == "true") {
          document.getElementById("infoChosen").innerHTML += "Plants\n";
        }
      }
    } else {
      /////////////AREA OF THE CODE THATS NOT WORKING///////////////////////
      y[count].onclick = (function(z) {
        sessionStorage.setItem(z, "true");
      })(y[count].id);
      /////////////AREA OF THE CODE THATS NOT WORKING////////////////////////
    }
    sessionStorage.setItem(y[count].id, "false");
  }
  sessionStorage.setItem("Plants", "true");
}
 			<div><button id="Grass" type="button" class="button">Send me more information about Grass</button></div>
		<div><button id="Molluscs" type="button" class="button">Send me more information about Green Molluscs</button></div>
		<div><button id="Sweaters" type="button" class="button">Send me more information about Green Sweaters</button></div>
		<div><button id="infoChosen" type="button" class="button">Things you want more information about </button></div>	
		<div id="displayInfo"></div>
		<div><a href="otherPage.html">Other Page</a></div>	

otherPage.html 只包含

    <a href="example.html">Example</a>  

【问题讨论】:

    标签: javascript html onclick session-storage


    【解决方案1】:

    您没有通过以下方式分配事件侦听器:

    y[count].onclick = (function(z) {
        sessionStorage.setItem(z, "true");
    })(y[count].id);
    

    因为 IIFE(立即调用的函数表达式)会立即计算为其返回值。因为他们不返回任何东西,onclick 将是undefined。您应该设置一个闭包,然后返回一个将分配给 onclick 属性的函数,如下所示:

    y[count].onclick = (function(z) {
        return function() { // return a function reference to be assigned to the onclick
            sessionStorage.setItem(z, "true");
        };
    })(y[count].id);
    

    【讨论】:

    • 我对我的代码进行了编辑,因为我没有意识到切换网页后 sessionStorage 会重置,我只是添加了一个链接,但我希望在切换页面时出现相同的内容,然后当我回来时
    • @Bob 如果您希望在关闭页面甚至关闭计算机后仍保留数据,请使用localStorage 而不是sessionStorage,因为sessionStorage 将在页面后被删除已关闭(会话丢失),但localStorage 将一直保留在那里,只要人类在地球上(比喻地说)或直到用户清除他的缓存,这将相当于世界末日(再次比喻地说)!
    • 这里是 localStoragesessionStorage 的 MDN 文档!
    • 在我关闭页面或关闭计算机后不会。我希望它在那之后被刷新。我只想在页面上的链接之间导航时保​​存它。窗口关闭后,我希望它刷新
    • @Bob 没有中间解决方案,localStorage 将尽可能长时间地保留在那里,或者sessionStorage 在页面离开后将丢失。您可以使用 localStorage 并在完成后清空它,或者在加载时清空它,这样您就有了一个可以填充的空间。
    猜你喜欢
    • 1970-01-01
    • 2012-01-04
    • 1970-01-01
    • 1970-01-01
    • 2011-10-02
    • 2016-01-16
    • 1970-01-01
    • 2011-04-16
    相关资源
    最近更新 更多