【问题标题】:Not able to append element无法附加元素
【发布时间】:2016-07-26 02:25:58
【问题描述】:

我设置了 2 个图像容器,它显示了从元素数组中随机生成的 2 个图像。当用户单击 2 个随机生成的图像中的任何一个时,将出现一个图像弹出窗口。此生成的图像弹出窗口;从第二个图像数组中,与已显示的随机生成的图像有关。

因此,这是正确的行为: - 随机生成的图片A和图片B显示在页面1中,当用户点击图片A时,会弹出图片A1,否则点击图片B时会弹出图片B1。

问题:

我已经成功实现了以下目标:

1.) 从数组 A 中随机生成 2 个图像并将其附加到初始的 2 个图像容器中

2.) 将数组 B 链接到数组 A 中生成的项目

我已经设置了一个控制台日志,并且能够看到当我从数组 A 中选择随机生成的图像时,第二个数组元素中的元素被正确调用

但是,此时,我无法将数组 B 中的项目附加到我的 html 正文中的图像弹出窗口中..

请帮忙

代码如下:

var BrandNameArray = ["lib/img/Brands/A.png", "lib/img/Brands/B.png", "lib/img/Brands/C.png", "lib/img/Brands/D.png"];

var OfferArray = ["lib/img/Offer/A.png", "lib/img/Offer/B.png", "lib/img/Offer/C.png", "lib/img/Offer/D.png"];
var random_BrandIndex, selectedOffer;

function ShowInitialBrand() {
  $('#BrandWinlist > img').each(function(i, img) {
    random_BrandIndex = Math.floor(Math.random() * BrandNameArray.length);
    //Assign Variable to generate random Brands
    var Brand = BrandNameArray.splice(random_BrandIndex, 1)[0];
    $(img).attr('src', Brand).show();
  });
}

function selectBrand(flag) {
  selectedOffer = OfferArray[random_BrandIndex];

  console.log("selectedOffer: " + selectedOffer);

  $("#P_Description").html(selectedOffer);
}
.GameWinBrand_Container {
  position: absolute;
  top: 950px;
  left: 286px;
  height: 250px;
  width: 500px;
  overflow: hidden;
}
.GameWinBrand_innerScroll {
  position: relative;
  width: 480px;
  font-size: 30px;
  text-align: justify;
  color: #ffffff !important;
  overflow: hidden;
}
.GameWinBrand_Container::-webkit-scrollbar-track {
  -webkit-box-shadow: inset 0 0 6px rgba(0, 0, 0, 0.3);
  border-radius: 12px;
  background-color: #ffffff;
}
.GameWinBrand_Container::-webkit-scrollbar {
  width: 12px;
  background-color: #5e5767;
}
.GameWinBrand_Container::-webkit-scrollbar-thumb {
  border-radius: 20px;
  -webkit-box-shadow: inset 0 0 6px rgba(0, 0, 0, .3);
  background-color: #5e5767;
}
.BrandMenu {
  background-color: #FFFFFF;
  filter: alpha(opacity=80);
  opacity: 0.8;
}
<!-- The first page where 2 randomly generated images are shown-->
<div class="GameWinBrand_Container">
  <div id="BrandWinlist" class="GameWinBrand_innerScroll">
    <img id="GameBrand_1" style="width:230px; height:230px; top:0px; left:0px; border:0px; outline:0px" onclick="selectBrand('1');">
    <img id="GameBrand_2" style="width:230px; height:230px; top:0px; left:330px; border:0px;" onclick="selectBrand('2');">
  </div>
</div>

<!-- The second page that will show the image in relation to the image that has been clicked-->

<div id="BrandDescription" class="BrandMenu" align="center" style="position:absolute; width:1080px; height:1920px; background-repeat: no-repeat; display: none; z-index=9; top:0px; margin:auto;">
  <img id="P_Description" style="position:absolute; width: 1080px; height:762px; top:500px; left:0px; z-index=99;">

</div>

【问题讨论】:

  • random_BrandIndex 是全局变量吗?如果不是,我不知道它是如何从函数selectBrand(flag)
  • 是的,它声明为全局变量

标签: javascript jquery arrays random


【解决方案1】:

如果您尝试在第二个中设置图像 URL 以显示来自 arrayB 的报价,您不想更改 HTML,您想设置 img 标记的 src 属性,

function ShowInitialBrand() {
  $('#BrandWinlist > img').each(function(i, img) {
   random_BrandIndex = Math.floor(Math.random() * BrandNameArray.length);
//Assign Variable to generate random Brands
var Brand = BrandNameArray.splice(random_BrandIndex, 1)[0];
$(img).attr({'src':Brand,'data-index':random_BrandIndex}).show();
  });
}

function selectBrand(flag) {
// assuming this is a callback function for the event handler, $(this) would be the image that's clicked.
  selectedOffer = $(this).attr('data-index');

  console.log("selectedOffer: " + selectedOffer);

  $("#P_Description").attr('src',selectedOffer);
}

【讨论】:

  • 好的,但是在这一点上,我已经尝试过了。与第一个阵列中生成的图像相反,第二个阵列生成一个独立的图像。意思是,当从第一个数组中单击 img/Brand/A.png 时,正确显示 img/Offer/C.png 时,应该显示 img/Offer/A.png。单击时,两个图像都从第二个数组播种相同的图像
  • 您需要单独存储每个图像的索引。当您的 each 循环运行时,它会更改 random_BrandIndex。然后,单击处理程序使用它来确定要显示的图像。这意味着您可以显示来自 arrayA 的 5 个图像,并且它将始终显示 arrayB 中最后一个图像的索引。您可以将索引存储在元素中,并在单击时获取它,以便获得与单击的图像相同的图像。
  • 我并没有真正关注你。因此,如果我必须存储,我将有一个变量:例如“var storeSelectedOffer”,我应该如何引用它?
  • 刚刚更新了答案,假设 selectBrand 是一个事件处理程序。您不想将变量存储在 JS 文件中,因为您必须知道商品和变量之间的关系。您还可以考虑获取点击报价的 src,将文件路径从 lib/img/Brands/a.png 更改为 lib/img/Offers/a.png`,因为它们的名称相同......这可能更容易。您不需要第二个数组,也不需要确保数组完美排列。
  • selectBrand 是一个事件处理程序。如果我要遵循这种方法,则不会显示任何内容。 $("#P_description") 将被空白。对不起,我只是不明白。
猜你喜欢
  • 2010-10-11
  • 1970-01-01
  • 2012-12-21
  • 1970-01-01
  • 2021-06-29
  • 1970-01-01
  • 1970-01-01
  • 2016-09-30
相关资源
最近更新 更多