使用cookie/复选框持久化图像可见性
Javascript Persisting image visibility with a cookie/checkbox
这是我在Stack Overflow上的第一个问题,我是Javascript新手(一直在自学Python以获得编码的感觉),并认为我应该尝试一下简单的web应用程序,看看将我的Python知识转化为Javascript是多么容易。
基本上,这个应用程序会让你知道你最喜欢的水果等什么时候上市。所以我有一个复选框列表在一个页面'fruitpicker.html'和对应的图像在另一个'seasonalguide.html'(图像只是图片的日历与水果季节阴影等)。
我有一个cookie,它保存复选框状态和一个外部JS,它根据相应的复选框状态切换图像可见性。大多数cookie是别人在网络上使用的,所以它工作得很好,但是图像vis JS给我带来了麻烦。
我已经写了我认为应该是什么,但它没有工作,无论我怎么修修补补,什么都没有。
我相信这是很愚蠢的东西,但无论如何,这是代码…
破碎的图像vs JS:
function toggleVisibility(checkId, imageId) {
var imgEl = document.getElementById(imageId);
var checkEl = document.getElementById(checkId);
if (checkEl.checked) {
imgEl.style.visibility="hidden";
imgEl.style.display="none";
}
else {
imgEl.style.visibility="visible";
imgEl.style.display="inline-block";
}
}
顺便说一下,如果行:var checkEl = document.getElementById(checkId);
被删除,代码工作,但图像状态不持久。这是我所能做到的。
fruitpicker.html中的几个复选框:
<html>
<head>
<title>Fruit Picker</title>
<script type="text/javascript" src="cookie.js"></script>
</head>
<body onload="restorePersistedCheckBoxes();">
<label for= "chklogo">Braeburn</label>
<input type= "checkbox"
id= "chkBraeburn"
onChange= "toggleVisibility('braeburnItem');
toggleVisibility('braeburnSeason');
persistCheckBox(this);" /><br>
<label for= "chklogo">Fuji</label>
<input type= "checkbox"
id= "chkFuji"
onChange= "toggleVisibility('fujiItem');
toggleVisibility('fujiSeason');
persistCheckBox(this);" /><br>
<label for= "chklogo">Golden Delicious</label>
<input type= "checkbox"
id= "chkgolldenDelicious"
onChange= "toggleVisibility('goldenDeliciousItem');
toggleVisibility('goldenDeliciousSeason');
persistCheckBox(this);" /><br>
<label for= "chklogo">Granny Smith</label>
<input type= "checkbox"
id= "chkGrannySmith"
onChange= "toggleVisibility('grannySmithItem');
toggleVisibility('grannySmithSeason');
persistCheckBox(this);"/><br/>
</body>
</html>
这是季节指南页面:
<html>
<head>
<script type="text/javascript" src="cookie.js"></script>
<script type="text/javascript" src="imageVisibility.js"></script>
</head>
<body>
<img id="imgGuide"
src="Images/Calendar/calendarHeader.png"
align="left"/>
<img id="braeburnItem"
src="Images/Produce/Fruit/BraeburnApple.png"
style="float:left; display:none;" />
<img id="braeburnSeason"
float="top"
src="Images/InSeason/InSeasonApr.png"
align="left"
style="display:none"/>
<img id="fujiItem"
src="Images/Produce/Fruit/FujiApple.png"
style="float:left; display:none;" />
<img id="fujiSeason"
float="top"
src="Images/InSeason/InSeasonMar.png"
align="left"
style="display:none;"/>
<img id="goldenDeliciousItem"
src="Images/Produce/Fruit/GoldenDeliciousApple.png"
style="float:left; display:none;"/>
<img id="goldenDeliciousSeason"
src="Images/InSeason/InSeasonFeb.png"
align="left"
style="display:none;"/>
<img id="grannySmithItem"
src="Images/Produce/Fruit/GrannySmithApple.png"
style="float:left; display:none;"/>
<img id="grannySmithSeason"
src="Images/InSeason/InSeasonApr.png"
align="left"
style="display:none;"/><br>
<table width="170px" height="70px">
<tr>
<td>
<a href="Main.html" id="back" title="about" class="button">
<img src="Images/Buttons/backButton.png" align="right">
</td>
</tr>
</table>
</body>
</html>
这个问题很长,所以我没有包括饼干,但如果它有用的话,我可以。任何帮助都是感激的,谢谢
我注意到两个问题。
首先,您正在使用两个独立的页面,但是document.getElementById
只能访问您所在页面的元素。所以,你的行:
var imgEl = document.getElementById(imageId);
var checkEl = document.getElementById(checkId);
必须失败,因为它们试图访问两个页面中的元素。无论您在哪个页面上运行脚本,其中一行都将返回null。你的问题没有描述两个独立的HTML文件如何相互关联,所以我不能帮助你解决这个问题(第一个加载第二个每当一个复选框被点击,或者两个页面加载在父页面的框架或iframe ?)。
第二个问题是你的toggleVisibility
函数是用两个参数定义的,但是你的代码(例如):
onChange= "toggleVisibility('grannySmithItem');
toggleVisibility('grannySmithSeason');
只调用一个参数的方法(两次)。如果我们谈论的是同一个函数(您可能在两个页面上的每个页面上都有不同的函数定义),那么该函数永远无法识别正确的图像,因为从未提供imageId
。
- D3在一个调用中绘制不同的SVG形状,没有可见性
- jQuery css可见性在load方法中不起作用
- 页面可见性API实际上支持操作系统屏幕锁定吗
- 有没有一种方法可以从隐藏的可见性(CSS,JS)中获得淡入效果
- JavaScript可见性不起作用('隐藏'起作用,'可见'不起作用)
- 从内部回调的可见性
- Openlayers 3为了可见性而绑定到Vector层的复选框没有任何作用
- 在另一个下拉列表中的选择上切换下拉列表中选项的可见性
- 如何禁用mouseout可见性=“;隐藏的“;对小于768px的介质的影响
- 在网格视图的自动刷新过程中,设置内部网格视图文本框的可见性
- 使用javascript的图像可见性
- 如何在 JavaScript 中更改图像可见性
- 隐藏可见性会隐藏图像,但仍会显示隐藏元素占用的空间
- 页面加载时,图像的可见性值是否设置为隐藏
- php中的图像和可见性
- 检查图像文件的可见性
- D3设置图像可见性=隐藏错误
- 将图像可见性设置为假
- 使用cookie/复选框持久化图像可见性
- 我将如何去计时与javascript图像的可见性,例如使用myTimer