Javascript替换img文件
我正在一个需要使用javascript的网站上工作。我需要Javascript来使用外部页面。我不太明白怎么做。我的计算机上有5个文件,需要能够替换图像文件的名称和alt。但我不能使用http 我有图像的代码:Javascript替换img文件,javascript,jquery,html,Javascript,Jquery,Html,我正在一个需要使用javascript的网站上工作。我需要Javascript来使用外部页面。我不太明白怎么做。我的计算机上有5个文件,需要能够替换图像文件的名称和alt。但我不能使用http 我有图像的代码: <img src="images/cablecar.jpg" width="480" height="270" alt="cable car turnaround" id="gallery" /> 在使用onmouseover时,我需要使用javascript将cabl
<img src="images/cablecar.jpg" width="480" height="270" alt="cable car turnaround" id="gallery" />
在使用onmouseover时,我需要使用javascript将cablecare和alt部分更改为新的图像文件和描述。
到目前为止,我掌握的代码是
Javascript:
function switchPix(file, desc){
var line = '<img src= asian.jpg width=\'480\' height=\'270\' alt= +desc+/>';
document.getElementById("pix").write(line);
}
功能开关PIX(文件,描述){
var行=“”;
document.getElementById(“pix”).write(行);
}
html:
我必须使用单词file来替换cablecar为asian,因此文件名为asian.jpg
最后,假设将鼠标放在五个链接中的一个上时,代码会将页面上的图像更改为另一个图像
对不起,如果这是一个有点混乱,我真的很难理解它自己,并没有得到足够的信息,以了解如何做自己 请尝试使用此代码
<figure id="pix" class="right">
<script src="scripts/gallery.js">
</script>
<img src="images/cablecar.jpg" width="480" height="270" alt="cable car turnaround" id="gallery" />
</figure>
<ul>
<!-- fix onmouseover (all small case) and string parameters -->
<li><a href="http://www.asianart.org/" onmouseover="switchPix('asian.jpg', 'Asian Market')">Asian Art Museum</a></li>
<figure id="pix" class="right">
<script src="scripts/gallery.js">
</script>
<img src="images/cablecar.jpg" width="480" height="270" alt="cable car turnaround" id="gallery" />
</figure>
<ul>
<!-- fix onmouseover (all small case) and string parameters -->
<li><a href="http://www.asianart.org/" onmouseover="switchPix('asian.jpg', 'Asian Market')">Asian Art Museum</a></li>
function switchPix(file, desc) {
var $elm = document.getElementById("gallery");
$elm.src = file;
$elm.alt = desc;
}