【发布时间】:2015-12-28 17:54:51
【问题描述】:
我必须使用 java 脚本更改图像的 src,我很确定我遇到了障碍,在 html 中我有 3 个 li 元素,id 是 mouseenter img 的来源。我觉得我很近,但到目前为止。到目前为止,这是我的代码。谢谢你的帮助!
Javascript:
var $ = function (id) {
return document.getElementById(id);};
window.onload = function () {
var links = document.getElementsByTagName("li"),
imgElements = document.getElementsByTagName("img"),
imgNode,
i,
URLone,
URLtwo,
link,
image;
for (i = 0; i < imgElements.length; i++) {
imgNode = imgElements[i];
}
imgNode.mouseenter = function () {
var img = this;
URLtwo = img.getAttribute('id');
img.src = URLtwo;
}
imgNode.mouseout = function () {
var img = this;
URLone = img.getAttribute('src');
img.src = URLone;
};
//preload
for (i = 0; i < links.length * 2; i++) {
link = links[i];
image = new Image();
image.src = link.src;
image = new Image();
image.src = link.id;
}};
HTML ::
<body>
<section>
<h1>Ram Tap Combined Test</h1>
<ul id="image_rollovers">
<li><img src="images/h1.jpg" alt="" id="images/h4.jpg"></li>
<li><img src="images/h2.jpg" alt="" id="images/h5.jpg"></li>
<li><img src="images/h3.jpg" alt="" id="images/h6.jpg"></li>
</ul>
</section>
工作 jQuery:
$(document).ready(function() {
$("#image_rollovers img").each(function() {
var oldURL = $(this).attr("src"); // gets the src attribute
var newURL = $(this).attr("id"); // gets the id attribute
// preload images
var rolloverImage = new Image();
rolloverImage.src = newURL;
$(this).hover(
function() {
$(this).attr("src", newURL); // sets the src attribute
},
function() {
$(this).attr("src", oldURL); // sets the src attribute
}
); // end hover
}); // end each
}); // end ready
【问题讨论】:
-
描述你的路障。有什么问题?
-
我认为这可能与我试图理解和使用的节点或逻辑有关。这周对我来说是新的,阅读后我仍然不完全理解它
-
你反对使用jQuery吗?还有,这是作业吗?如果是这样,那么关于 SO 的规则会有些不同;用作业标签标记它。
-
虽然它不允许我这样做,因为我刚刚注册并且没有 1500 积分,否则我会。我不允许使用 jQuery 来完成这项任务,因为它已经在书中。
-
什么不起作用?原图不显示?没有显示
onmouseover?不显示onmouseout?
标签: javascript html mouseenter mouseout