如何设置一个变量直接替换类? [英] How do I set a variable to direct alternative classes?

查看:104
本文介绍了如何设置一个变量直接替换类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是重新发布 上一个问题 。其他问题没有制定得不够好。我不会删除另一个问题,因为它可能对其他人有用。

Hello全部,
我有一个javascript / jquery脚本,包含cookie和指向背景图像类的身体标记,通过< body id ='body_bg'> 。这个脚本的小部分例如:

This is a reposting of a previous question. Other question wasn't formulated good enough. I won't delete the other question, because it might be usable for others.

Hello All,
I have a javascript/jquery script, containing cookies and directing background-image-classes for the body tag, via<body id='body_bg'>. Small portion of this script for example:

$('.somebuttons_class').live('click', function(){
  $('#body_bg').removeClass('image10').addClass('image1');
  $.cookie('bg_for_the_day', 'image1', { path: '/', expires: 1 });
});

在这段脚本中,用户正在显示一个可选的'image10'到'body_bg'将其更改为默认图像'image1'。
此脚本现在位于我的'mypage.html'页面的头部。我想将它移到外部js页面。

问题是:
你可以看到,我已经通过名称调用了主体的默认类, 'image1'。但myotherpage.html的默认类是image2,而yetanother.html的默认类是image3。这意味着我必须将整个脚本复制/过去到myotherpage.html,并将所有的'image1'改为'image2'。这是很多的dubble代码,但它会工作。然而,如图所示,我的cookie给我回值='image1'。


问题:我如何设置一个变量say'default',而不是class =' image1'或'image2',在我的javascript?如果页面是mypage.html,我如何输入脚本将默认翻译为image1,如果页面是myotherpage.html,则使用image2?

给你一个我的意思的印象:

In this piece of script the user is displaying an optional 'image10' to 'body_bg', but wants to change this back to the default image of 'image1'.
This script is now in the head section of my 'mypage.html' page. I would like to move this to an external js-page.

Problem is: as you can see, I have called the default class for the body by name, 'image1'. But the default class for 'myotherpage.html' is 'image2' and for 'yetanother.html' it is 'image3'. This means I have to copy/past the whole script to 'myotherpage.html' and change all the 'image1's into 'image2's. That is a lot of dubble code, but it would work. However, as shown, my cookies give me back value='image1' as well. So that's not gonne work for any of my other pages at all!

Question: How do I set a variable of say 'default', instead of class='image1' or 'image2', in my javascript? And how do I input the script to translate 'default' back to 'image1' if the page is 'mypage.html', but use 'image2' if the page is 'myotherpage.html'?
Just to give you an impression of what I mean:

var currentPage =  window.location.pathname;
var default;
if(currentPage == "mypage.html"){
  default = "image1";
}
if(currentPage == "myotherpage.html"){
  default = "image2";
}

然后上一个脚本看起来像这样:

Then the previous script is going to look something like this:

$('.somebuttons_class').live('click', function(){
  $('#body_bg').removeClass('image10').addClass('default');
  $.cookie('bg_for_the_day', 'default', { path: '/', expires: 1 });
});

我现在已经学习了很多关于javascript的东西, )我先感谢你的帮助。


编辑:答案后,我得到了我再试一次。我没有得到它的工作。我决定制作一个简单的实际脚本版本,并在这里发布。请检查一下,如果你有一些时间。
您可以 在此下载测试页 。欢迎每一个帮助!

I, by now have learned quite a bit about javascript, this feels like getting to the next level ;) I thank you in advance for your help.

After the answer I got I tried again. I didn't get it working. I decided to make a simplefied version of the actual script and post it here. Please check it out, if you have some time to spare.
You can download the testpage here. Every help is welcome!

推荐答案

您的代码看起来分开这行:

Your code looks ok apart for this line:

 $.cookie('bg_for_the_day', 'default', { path: '/', expires: 1 });

这应该是:

 $.cookie('bg_for_the_day', default, { path: '/', expires: 1 });

,以便cookie的值是您的变量(在您的代码中,cookie的值总是default

so that the value of the cookie is your variable (in your code the value of the cookie is always "default"

这篇关于如何设置一个变量直接替换类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆