[英]Can I change all the pictures on my entire website (2 pages), using just JavaScript and a button element?
I am trying to learn how JavaScript functions can affect not just the page the visitor is currently on, but all the pages on the website. 我正在尝试学习JavaScript函数如何不仅会影响访问者当前所在的页面,而且会影响网站上的所有页面。 I have made a simple site with 2 pages, one with a blue face and one with a green one.
我制作了一个简单的网站,其中包含2页,其中一页带有蓝色的页面,另一页带有绿色的页面。 When you click on the 2 buttons, the blue face will either become a smiling face, or an angry face.
当您单击两个按钮时,蓝色的脸会变成笑脸或愤怒的脸。
My question is, how do I target the green face on the other page to do the same as the blue face? 我的问题是,我如何将另一页上的绿色面作为蓝色面的目标? And is it possible to do with JavaScript alone?
并且可以单独使用JavaScript吗? Also, if I have pressed the happy button and made the blue face smile, it becomes neutral again if I go back and forth between the two pages.
另外,如果我按下了快乐按钮并使蓝色的脸微笑,那么如果我在两页之间来回切换,它将再次变为中性。
Any tips on how I can solve this? 关于如何解决此问题的任何提示? Suggestions to tutorials that teach the basics of what I am trying to do would also be great.
指导我尝试做的基础知识的教程的建议也很好。 I suspect this has got something to do with localStorage.
我怀疑这与localStorage有关。
I have tried to play around with localStorage, but could not make it work in the project I am working on. 我尝试使用localStorage,但无法在我正在处理的项目中使用它。
Here is the code for index.html (main page with the buttons and blue face): 这是index.html(带有按钮和蓝色面孔的主页)的代码:
<div class="container">
<nav>
<a href="other_page.html">Link to other page</a>
</nav>
<img id="indexPic" src="images/neutral_blue.png" alt="">
<button id="toSmiley" type="button" name="button">Make images on all pages into smiley faces</button>
<button id="toAngry">Make images on all pages into angry faces</button>
</div><!-- container -->
<script src="smiley.js" type="text/javascript"></script>
Here is the code for other_page.html: 这是other_page.html的代码:
<div class="container">
<nav>
<a href="index.html">Link to main page</a>
</nav>
<img id="otherPic" src="images/neutral_green.png" alt="">
</div><!-- container -->
<script src="smiley.js" type="text/javascript"></script>
And here is my JavaScript file: 这是我的JavaScript文件:
let smileyBtn = document.getElementById('toSmiley');
let angryBtn = document.getElementById('toAngry');
function allSmiles(a) {
document.getElementById('indexPic').src = a;
}
function longFace(b) {
document.getElementById('indexPic').src = b;
}
smileyBtn.onclick = function () {
allSmiles("images/smiley_blue.png");
}
angryBtn.onclick = function () {
longFace("images/angry_blue.png");
}
I also have posted the project on my Github account here: https://github.com/jjberg83/change_all_pics 我还在这里在我的Github帐户上发布了该项目: https : //github.com/jjberg83/change_all_pics
If you want to pass data between pages you will need to use LocalStorage, SessionStorage or a Cookie. 如果要在页面之间传递数据,则需要使用LocalStorage,SessionStorage或Cookie。 This article explains the difference quite nicely scotch.io/@PratyushB/local-storage-vs-session-storage-vs-cookie
本文很好地解释了差异scotch.io/@PratyushB/local-storage-vs-session-storage-vs-cookie
For your example I would recommend SesionStorage d eveloper.mozilla.org/en-US/docs/Web/API/Window/sessionStorage 对于您的示例,我建议使用SesionStorage d eveloper.mozilla.org/en-US/docs/Web/API/Window/sessionStorage
So you would need to save the 'mood image' whenever it gets set eg sessionStorage.setItem('mood-image', 'images/smiley_blue.png');
因此,只要设置了“情绪图像”,就需要保存它,例如
sessionStorage.setItem('mood-image', 'images/smiley_blue.png');
Then, on page load, you could check to see if it had been set previously then use the value accordingly: 然后,在页面加载时,您可以检查它是否先前已设置过,然后相应地使用该值:
var presetImage = sessionStorage.getItem('mood-image');
if( presetImage ){
// do your stuff, setting the relevant src attributes to the preset-image
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.