简体   繁体   English

Javascript .innerHTML仅影响父div

[英]Javascript .innerHTML only affecting parent div

My code is supposed to create a new game, to which you can customize your students and what not. 我的代码应该创建一个新游戏,您可以自定义学生,而其他则不能。 That's not the problem, when you load the game or after finish customizing though, there is an error. 这不是问题,当您加载游戏或完成自定义后,会出现错误。 For whatever reason on Chrome my images won't load. 无论出于什么原因,我的图片都无法加载。 Nor will it even edit the element that I want to change. 它甚至也不会编辑我要更改的元素。 This is the update/running code that should work: 这是应该工作的更新/运行代码:

function startUp() {
  if (saving) {
    $('#newGame-settings').fadeOut(500);
    setTimeout(function() {
      $("#mainScreen").fadeIn(1500);
    }, 500);
    saving = false;
  } else {
    $('#Start_Menu_Container').fadeOut(500);
    setTimeout(function() {
      $("#mainScreen").fadeIn(1500);
    }, 500);
  }


  var container = "";
  for (var i = 0; i < students.length; i++) {
    container += ("<div class='studentListC' style='background-image: url(\"" + students[i].imgSrc + "\")'>" + students[i].name + "</div>");
  }
  var studentList = document.getElementById("studentList");
  studentList.innerHTML = container;
}

It should just show the main screen, which it does, and then edit the studentList id element. 它应该只显示它​​的主屏幕,然后编辑studentList id元素。 Which it does not . 没有 If anybody could help me that would be great! 如果有人可以帮助我,那就太好了!

Also, something useful to probably know is that if I make it so it adds to the mainScreen id (The parent element of studentList) it sort of works, except the picture still does not show. 另外,可能有用的一点是,如果我这样做,它将添加到mainScreen id(studentList的父元素)中,除了图片仍然没有显示之外,它的工作方式还可以。 So I may also have an error with the strings in the style. 因此,样式中的字符串也可能有错误。

Link to all code: http://jsbin.com/joneyi/edit?html,css,js,output 链接到所有代码: http : //jsbin.com/joneyi/edit?html,css,js,output

In regards to the problem of it not populating the studentList element, it actually is populating a studentList element, just not the one you want (there is still a hidden div on the page called studentList. I believe you just need to make sure you are selecting the studentList element under the mainPage element. (Forgive me for not providing the code to do that, I'm a but rusty on my javascript syntax.) 在问候它不填充studentList元素的问题,它实际上是填充一个 studentList元素,只是没有你想要的(还有叫studentList在页面上隐藏的div的人。我相信,你只需要确保你是选择mainPage元素下的studentList元素(请原谅我未提供执行此操作的代码,但我对JavaScript语法有些不满。)

As far as the images, I think the issue is the nested double quotes: 至于图像,我认为问题是嵌套的双引号:

<div class="studentListC" style="background-image: url("https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcQy51udfhp-M17O5xe9wdQNpzq-Rf34cLMcCZ9hqkOIF2PEyKKa")">Brayton</div>

I see your code is actually using single quotes for the whole style attribute, but I think they may be getting converted to double quotes somewhere (possibly an implicit conversion?) Anyway try using single quotes to enclose the url value. 我看到您的代码实际上是对整个style属性使用单引号,但是我认为它们可能会在某处转换为双引号(可能是隐式转换?)。无论如何,请尝试使用单引号将url值括起来。

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

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