- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
Javascript 新手。这是一个类似抽认卡的程序,应该有图像,然后你猜名字。我正在使用 setAttribute 但图像没有填充页面。我应该做一个 for 循环来选择图像还是我只是做我所做的事情?我需要使用 setAttribute 但我不知道为什么图像不会填充。非常感谢您的帮助。
function populateImages() {
var newDiv = document.createElement('div');
newDiv.setAttribute('class', 'frame');
newDiv.setAttribute('src', personArray.url);
newDiv.setAttribute('onclick', 'promptForName(this)');
newDiv.setAttribute('onmouseover', 'styleIt(this)');
newDiv.setAttribute('onmouseout', 'unStyleIt(this)');
newDiv.setAttribute('id', '1');
document.getElementById('pic-grid').appendChild(newDiv);
}
这是我的 JS Bin
最佳答案
要获取所有图像图标,您需要迭代数组。当您设置值时,应该放置一个不带引号的数组。将您的 div 更改为 img
标签。我已经对您的代码进行了更正,希望这会有所帮助。
var currentId = "";
var x = 0;
var y = 0;
var personArray = [{
firstname: "Ann",
url: "http://www.tuktukdesign.com/wp-content/uploads/2017/12/person-icon.jpg"
},
{
firstname: "Jane",
url: "http://www.tuktukdesign.com/wp-content/uploads/2017/12/person-icon.jpg"
},
{
firstname: "John",
url: "http://www.tuktukdesign.com/wp-content/uploads/2017/12/person-icon.jpg"
},
{
firstname: "Joe",
url: "http://www.tuktukdesign.com/wp-content/uploads/2017/12/person-icon.jpg"
}
];
function populateImages() {
for (var i = 0; i < personArray.length; i++) {
var newDiv = document.createElement('img');
newDiv.setAttribute('class', 'frame');
newDiv.setAttribute('src', personArray[i].url);
newDiv.setAttribute('onclick', 'promptForName(this)');
newDiv.setAttribute('onmouseover', 'styleIt(this)');
newDiv.setAttribute('onmouseout', 'unStyleIt(this)');
newDiv.setAttribute('id', '1');
document.getElementById('pic-grid').appendChild(newDiv);
}
}
function promptForName(element) {
document.getElementById('response').value = "";
document.getElementById('message').innerHTML = "";
document.getElementById('prompt').style.display = 'block';
currentId = element.id;
x = element.offsetLeft;
y = element.offsetTop;
x = x + 20;
y = y + 20;
document.getElementById('prompt').style.position = "absolute";
document.getElementById('prompt').style.top = y;
document.getElementById('prompt').style.left = x;
document.getElementById('response').focus();
}
function styleIt(element) {
element.parentNode.style.backgroundColor = 'aqua';
}
function unStyleIt(element) {
element.parentNode.style.backgroundColor = 'white';
}
function checkAnswer() {
if (document.getElementById('response').value == personArray[currentId].firstname) {
document.getElementById(currentId).className = "opClass";
document.getElementById(currentId).removeAttribute("onclick");
document.getElementById(currentId).removeAttribute("onmouseover");
var divVar = document.createElement('div');
divVar.setAttribute('id', currentId + 'name');
document.getElementById('pic-grid').appendChild(divVar);
var textNode = document.createTextNode(personArray[currentId].firstname);
divVar.appendChild(textNode);
document.getElementById(currentId + 'name').style.position = "absolute";
document.getElementById(currentId + 'name').style.top = y;
document.getElementById(currentId + 'name').style.left = x;
document.getElementById('prompt').style.display = 'none';
document.getElementById(currentId).parentNode.style.backgroundColor = 'white';
document.getElementById('response').value = "";
document.getElementById('message').innerHTML = "";
} else {
if (document.getElementById('message').innerHTML == "Wrong!") {
document.getElementById('message').innerHTML = "Incorrect answer!"
} else {
document.getElementById('message').innerHTML = "Wrong!"
}
}
return false;
}
body,
header {
text-align: center;
font-family: arial;
font-size: 1em;
background-color: silver;
}
.frame {
padding: 25px;
background-color: white;
border: solid 1px black;
display: inline-block;
}
#prompt {
background-color: aqua;
padding: 10px;
display: none;
}
img {
cursor: pointer;
}
.opClass {
opacity: 0.4;
filter: alpha(opacity=40);
/* For IE8 and earlier */
cursor: default;
}
<body onload="populateImages()">
<header>
<h2>Class Flashcards</h2>
<h3>Click on a student to guess their name</h3>
</header>
<div id="pic-grid">
</div>
<div id="prompt">
What is this student's name?<br />
<form onsubmit="return checkAnswer()">
<input type="text" id="response" name="quizInput">
</form>
<div id="message"></div>
</div>
</body>
关于javascript - 使用对象将属性设置为 img,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55565874/
我想替换每一个 带结束标记 字符串中的标记。该字符串实际上是一个 html 文档,其中 img 标记由我生成,并且始终如下所示: Src 是用户输入,所以它可以是任何东西。我做了一个正则表达式,不确
我使用数组通过 getElementsByClassName 存储我所有的 imgs。 我需要知道哪个 img 被点击或 mouseover/mouseout,所以我使用循环来检查哪个 img 被点击
我正在尝试使用图像制作一款类似 Match3 的游戏,但我无法进行比较。我正在为固定数量的 atm 执行此操作,只是为了让它正常工作,稍后应该在 foreach 循环中。如果有什么区别的话,该函数位于
我希望你能帮助我:) 我想定义 img 的高度,相对于图像的“实际”宽度。但宽度是动态的,因为它占父对象的百分比(wxample 的浏览器窗口)。 为什么我需要高度?:没有高度它工作正常,但我需要它,
我知道这个话题被讨论了很多,但我找不到任何适合我的解决方案。所以,我的代码大致是: var img =me.images[curID] var f = function() { var
我试图在一个页面上列出多个图像,但当您单击图像时,它会以模式打开。 它适用于第一张图片,但不适用于其他图片,我假设这是一个 JS 问题,我尝试设置一个空的 var,然后将其设置为获取元素 ID(每个
任务:我们正在通过 HttpWebRequest 抓取 HTML 内容(约 6,000 个调用)。该字符串经过修剪并存储在 SQL Server 2014 数据库中,以便作为 XML 进行处理。 问题
我从上面得到这个错误,不知道如何避免它。我的目的是获取屏幕截图,然后对其进行模板匹配,以查看此时屏幕上是否显示图标。到目前为止,它只是图标的位置。我的代码: #include "opencv2/hig
我有一个包含图像的容器,该图像是从应用程序加载的,因此容器的尺寸是已知的,而图像会有所不同。 我目前有以下 css: #secondary_photos { height: 100px; wi
我正在尝试设置一个随分辨率缩放但看起来仍然不错的页面背景..这就是我正在使用的.. 站点是http://www.gd-gaming.com/wordpress ,如果你用 Firebug 检查它,它只
目前我有 如何删除包装 img 标签的 p 标签? 所以我可以得到.. 最佳答案 使用 $('p > img').unwrap(''); 这将删除 img 周围的所有 p。您应该使用 cl
我想要动画 3 .svg图片: 和css : .sequence { position: relative; } .sequence img { position: ab
我有外部 RSS 提要填充以下重复出现的类 elements 。 {teaserImage} {teaserImage} {teaserImage} 我想简单地获取 :first 实例,该实例也是来自
这是一个独特的问题: 我不想使用浏览器 JavaScript 来解决这个问题,请继续阅读... 我要转换 通过编译应用程序( ng build 或 ng serve )到 Base64 img 标签,
悬停在 中的第一张图片上标记,我需要使用 CSS 增加第二张图片的不透明度。我试过使用 +和 ~运营商,无法让它发挥作用。任何帮助将不胜感激。 最佳答案 a:hover + img
我已经尝试解决这个问题有一段时间了,但我迷路了,有人吗? for(var i=0; i<10; i++) { var Img = new Image(); Img.onload = (
这就是我想要实现的目标: 当用户将鼠标悬停在较小的图像之一上时: 较小的图像 + 文本应替换较大的图像 + 文本。 当用户没有悬停时;将大集返回到其原始图像和文字。 这就是我到目前为止所拥有的。它没有
我知道如何在 php 中执行此操作,但我需要在 javascript/jquery 中完成此操作。 我正在尝试执行以下操作: $('#NewBox').html( $('#OldBox').html(
我正在使用一个 CMS (ExpressionEngine),它将段落标签包裹在图像周围。我正在使用响应式图像(最大宽度:100%),并且由于我还在段落上定义宽度,因此它会导致问题。我想使用 jQue
Tinymce 正在删除我的 img 结束标记并生成无效的 xhtml。 它变成了这个 进入这个 我也在使用 codemagic,但是在查看 html 时它仍然显示 .我也试过包括 , 但输出是
我是一名优秀的程序员,十分优秀!