简体   繁体   English

是否可以仅使用JavaScript和按钮元素来更改整个网站(两页)上的所有图片?

[英]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.

相关问题 如何在我网站的所有不同页面上运行JavaScript函数? - How can I run a JavaScript function across all of my website different pages? 如何在我的网站上旋转图片? - How can I rotate pictures on my website? 如何在我的网站上的所有页面上进行搜索 - How can I search across all pages on my website 使用 JS 关闭我整个网站内所有页面的音频 - Turn off audio in all pages inside my entire website with JS 我正在使用的轮播中的图片不适合整个屏幕 - The pictures in my carousel that I am using do not fit the entire screen 如何使主题按钮更改器影响网站上的所有页面而不仅仅是主页? - How do i make my theme button changer affect all pages on my site not just the homepage? 如何使用 javascript 更改字符串的一部分中的文本? - How can I change the text in just a part of my string with javascript? 如何使我的JavaScript在网站上运行? (当前代码仅在网站上无法在Fiddle上运行) - How can I get my JavaScript to work on website? (Current code is working on Fiddle just not on website) 有没有办法用其他网站的图片自动填充我的网页? - Is there a way that I can autopopulate my webpage with pictures from another website? 我如何获得一个按钮来更改样式表,保存到本地存储,并显示在网站的所有页面上? - How would I get a button to change stylesheets, be saved to local storage, and appear on all pages of a website?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM