Javascript替换img文件

时间:2016-11-05 21:56:53

标签: 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将cablecare和alt部分更改为新的图像文件和描述。 我到目前为止的代码是 使用Javascript:

        function switchPix(file, desc){
        var line = '<img src= asian.jpg  width=\'480\' height=\'270\' alt= +desc+/>';
        document.getElementById("pix").write(line);
        }

HTML:

<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>
      <li><a href="http://www.asianart.org/" onmouseOver="switchPix(asian, Asian Market)">Asian Art Museum</a></li>

我必须使用word文件用亚洲代替cablecar,因此文件名是asian.jpg 最后,当鼠标放在五个链接中的一个上时,代码可能会将页面上的图像更改为另一个图像。

很抱歉,如果这有点令人困惑,我自己很难理解它,并且没有给出足够的信息来了解如何自己动手。

1 个答案:

答案 0 :(得分:1)

您好试着使用此代码

<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>

和像这样的javascript代码

function switchPix(file, desc) {
var $elm = document.getElementById("gallery"); 
$elm.src = file;
$elm.alt = desc;
}
相关问题