简体   繁体   English

使用Java脚本更改CSS元素

[英]Changing CSS elements using Javascript

Using javascript, I'm trying to change the background image of a div class when particular url params are entered. 使用javascript,当输入特定的URL参数时,我试图更改div类的背景图像。 However it isn't switching the image in the background. 但是,它不会在后台切换图像。

Example url: 网址示例:

 example.com?campaignid=street

The HTML HTML

<div class="row-background-79134">
    <p>Some content here</p>
</div>

The script is here: 脚本在这里:

<script>
function getQueryParams(qs) {
    qs = qs.split('+').join(' ');
    var params = {},
        tokens,
        re = /[?&]?([^=]+)=([^&]*)/g;
    while (tokens = re.exec(qs)) {
        params[decodeURIComponent(tokens[1])] = decodeURIComponent(tokens[2]);
    }
    return params;
}

var query = getQueryParams(document.location.search);
var myCampaign = query.campaignid;


if (myCampaign == "street"){
   var elems = document.getElementsByClassName('row-background-79134');
    for(var i = 0; i < elems.length; i++) {
    elems[i].style.background = 'url(https://example.com/image1.jpg) no-repeat center center;'
}

if (myCampaign == "plumber"){
   var elems = document.getElementsByClassName('row-background-79134');
for(var i = 0; i < elems.length; i++) {
elems[i].style.background = 'url(https://example.com/image2.jpg) no-repeat center center;'
}

if (myCampaign == "cowboy"){
   var elems = document.getElementsByClassName('row-background-79134');
    for(var i = 0; i < elems.length; i++) {
    elems[i].style.background = 'url(https://example.com/image3.jpg) no-repeat center center;'
}

if (myCampaign == "hairdresser"){
   var elems = document.getElementsByClassName('row-background-79134');
for(var i = 0; i < elems.length; i++) {
elems[i].style.background = 'url(https://example.com/image4.jpg) no-repeat center center;'
}

</script>

Any help on this would be great. 任何帮助都会很棒。

Each of these blocks 这些块中的每一个

if (myCampaign == "street"){
   var elems = document.getElementsByClassName('row-background-79134');
    for(var i = 0; i < elems.length; i++) {
    elems[i].style.background = 'url(https://example.com/image1.jpg) no-repeat center center;'
}

Should be re-written with an extra brace, like this 应该像这样用大括号重写

if (myCampaign == "street"){
   var elems = document.getElementsByClassName('row-background-79134');
   for(var i = 0; i < elems.length; i++) {
       elems[i].style.background = 'url(https://example.com/image1.jpg) no-repeat center center;';
   } // added this
}

Also consider adding some logging. 还可以考虑添加一些日志记录。 Use console.log to show you what's going on, and open your browser console to read the output. 使用console.log显示发生了什么,然后打开浏览器控制台以读取输出。 You probably want to know what the value of myCampaign is, for instance. 例如,您可能想知道myCampaign的值是什么。

console.log(myCampaign);

will help, near the top. 在顶部附近会有所帮助。 There are other ways to make the code a bit easier to read/maintain, but getting the braces right should be the first step. 还有其他方法可以使代码更易于阅读/维护,但是正确使用括号应该是第一步。

Your code if statements are missing a closing bracket } . 您的代码if语句缺少右括号} However, the major problem here is that your code is much larger than it should be, and is a redundant mess. 但是,这里的主要问题是您的代码比应有的要大得多,并且是一团糟。

To fix this, it is a much better idea to store the campaign names and image values in a object, then have one if statement: 要解决此问题,最好将广告系列名称和图像值存储在一个对象中,然后使用一个 if语句:

 //This would be the value you get from the URL, I've replaced it with a prompt for demo purposes var campaignName = prompt("campaign name? (leave blank for default)") || "street"; //Object containing a name/image pair for each of the campaigns var campaigns = { street: "image1.jpg", plumber: "image2.jpg", cowboy: "image3.jpg", hairdresser: "image4.jpg" }; //If a campaign with the name exists if (campaigns[campaignName]) { //Select elements by class name var elems = document.getElementsByClassName('row-background-79134'); //Loop through elements for (var i = 0; i < elems.length; i++) { //Since we have the other styles added with CSS, we only have to set the background-image elems[i].style.backgroundImage = 'url(https://example.com/' + campaigns[campaignName] + ')'; //Logging for demo purposes console.log("Added image to", elems[i]); } } 
 .row-background-79134 { background-repeat: no-repeat; background-position: center; } 
 <div class="row-background-79134"></div> <div class="row-background-79134"></div> <div class="row-background-79134"></div> <div class="row-background-79134"></div> 

Much cleaner, simpler, and easier to debug. 更干净,更简单,更容易调试。

If you want to add a new campaign/image, just add it to the campaigns object: 如果要添加新的广告系列/图像,只需将其添加到Campaigns对象:

var campaigns = {
  street: "image1.jpg",
  plumber: "image2.jpg",
  cowboy: "image3.jpg",
  hairdresser: "image4.jpg",
  chef: "image5.jpg",
};

Try this, There are syntax issues in your code. 试试这个,您的代码中有语法问题。 You did not close your brackets properly. 您没有正确合上括号。

if (myCampaign == "street"){
   var elems = document.getElementsByClassName('row-background-79134');
   for(var i = 0; i < elems.length; i++) {
   elems[i].style.background = 'url(https://example.com/image1.jpg) no-repeat center center;'
   }
}

if (myCampaign == "plumber"){
   var elems = document.getElementsByClassName('row-background-79134');
    for(var i = 0; i < elems.length; i++) {
     elems[i].style.background = 'url(https://example.com/image2.jpg) no-repeat center center;'
    }
}

if (myCampaign == "cowboy"){
  var elems = document.getElementsByClassName('row-background-79134');
  for(var i = 0; i < elems.length; i++) {
  elems[i].style.background = 'url(https://example.com/image3.jpg) no-repeat center center;'
  }
}

if (myCampaign == "hairdresser"){
   var elems = document.getElementsByClassName('row-background-79134');
   for(var i = 0; i < elems.length; i++) {
     elems[i].style.background = 'url(https://example.com/image4.jpg) no-repeat center center;'
   }
}

In each if bracket, youre missing a "}" which should close this block. 在每个if括号中,您都缺少一个“}”,应将其关闭。 In addition to that, next to the background assignment you are missing a ";", 除此之外,在背景分配旁边您还缺少“;”,

wrong: 错误:

if (myCampaign == "cowboy"){
  var elems = document.getElementsByClassName('row-background-79134');
  for(var i = 0; i < elems.length; i++) {
     elems[i].style.background = 'url(https://example.com/image3.jpg no-repeat center center;'
  }

right: 对:

if (myCampaign == "cowboy"){
  var elems = document.getElementsByClassName('row-background-79134');
  for(var i = 0; i < elems.length; i++) {
     elems[i].style.background = 'url(https://example.com/image3.jpg no-repeat center center;';
  }
}

If you want to make your current code shorter, use jQuery. 如果要缩短当前代码,请使用jQuery。

JavaScript: JavaScript:

var elems = document.getElementsByClassName('row-background-79134');
for(var i = 0; i < elems.length; i++) {
 elems[i].style.background = 'url(https://example.com/image4.jpg) no-repeat center center;';
}

jQuery jQuery的

$(".row-background-79134").css("background","url('https://example.com/image4.jpg') no-repeat center center");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM