我必須更改使用Java腳本的圖像的src,我非常確定我打了一個路障,在html中我有3個li元素,而id是mouseenter img的來源。我覺得我很接近,但到目前爲止。繼承我的代碼到目前爲止。謝謝你的幫助!如何更改圖像的來源W/JS
的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
描述你的路障。問題是什麼? – markbernard
我認爲這可能與我試圖理解和使用的節點或邏輯有關。它本週對我來說是新的,閱讀後我仍然不完全明白它 – IntheMountainair
你是否反對使用jQuery?另外,這功課呢?如果是這樣,關於SO的規則有點不同;用作業標籤標記它。 – levininja