首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MouseEnter / MouseOut事件

MouseEnter / MouseOut事件
EN

Stack Overflow用户
提问于 2017-02-10 00:53:02
回答 3查看 82关注 0票数 1

我一直在网上阅读文章,看YouTube视频-我迷路了。这是我尝试过的最后一段代码,在您阅读这篇文章时,这些代码可能已经改变了。看上去很简单,我不明白我做错了什么?我的头脑就是无法理解这一点。有什么帮助吗?当鼠标在(id)上时,我试图用(Id)中的图像替换图像(src)。现在,我真的只是想得到一个警告,当我鼠标对图像。什么都行!

*更新的代码*

这个刚进去!我是个笨蛋。Javascript文件没有正确定向,丢失了子文件夹。还在挣扎,但至少现在我的滚动是可行的。手掌到前额

HTML:

代码语言:javascript
复制
<!DOCTYPE HTML>
    <html lang="en">
    <head>
<meta charset="utf-8">
<title>Image Rollovers</title>
<link rel="stylesheet" href="styles/main.css">
<script src="js/rollover.js"></script>
    </head>

    <body>
    <h1>Rollover Test</h1>
    <ul id="rollover_test">
        <li>
            <img src="images/h1.jpg" alt="img1" id="images/h4.jpg" onmouseover="MouseOver('img1');" onmouseout="MouseOut('img1')">
        </li>
        <li>
            <img src="images/h2.jpg" alt="img2" id="images/h5.jpg">
        </li>
        <li>
            <img src="images/h3.jpg" alt="img3" id="images/h6.jpg">
        </li>
    </ul>        
    </body>
    </html>

Javascript:

代码语言:javascript
复制
var $ = function (id) {
return document.getElementById(id);
};

function MouseOver(id){ 
// I'm trying to figure out the syntax in here to swap the id and src tags
alert($("id").src);

};

function MouseOut(id){
alert("out");
}

window.onload = function () {
//preload images
var links = document.getElementsByTagName("li");
var i, link, image;
for (i=0; i<links.length; i++) {
    links = links[i];
    image = new Image();
};
};
EN

回答 3

Stack Overflow用户

发布于 2017-02-10 01:04:44

实际上,这三行简单的代码就足以让它工作。

代码语言:javascript
复制
$("img").on('mouseenter', function() {
  $(this).attr("src", $(this).attr('id'));
});
代码语言:javascript
复制
img {
  width: 100px;
  height: 100px;
}
代码语言:javascript
复制
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h1>Rollover Test</h1>
<ul id="rollover_test">
  <li>
    <img src="http://hd-wall-papers.com/images/wallpapers/stock-image/stock-image-15.jpg" alt="Img1" id="http://studio7designs.com/wp-content/uploads/free-stock-nature-photos.jpg">
  </li>
  <li>
    <img src="http://studio7designs.com/wp-content/uploads/free-stock-nature-photos.jpg" alt="Img2" id="http://www.jfcsmonmouth.org/Resources/Pictures/investing-in-stocks3---ticker-symbols_s600x600.jpg">
  </li>
  <li>
    <img src="http://www.jfcsmonmouth.org/Resources/Pictures/investing-in-stocks3---ticker-symbols_s600x600.jpg" alt="Img3" id="http://hd-wall-papers.com/images/wallpapers/stock-image/stock-image-15.jpg">
  </li>
</ul>

票数 1
EN

Stack Overflow用户

发布于 2017-02-10 01:20:51

在您的HTML中,id属性已经更改为data-id,因为最好保留id属性以进行css标识。

通过协作使用mouseenter和mouseleave,下面的代码段将查看遇到的每个图像,并将其src属性转换为附加到该图像的临时data-temp属性。

希望这些片段注释是不言自明的。

代码语言:javascript
复制
$("li").find('img').on({
  mouseenter: function() {
    $this = $(this); // get the current img object
    var src = $this.attr('src'), // get the current src
      id = $this.attr('data-id'); //get the alternative src
    $this.data('temp', src); // store in a new temporary data attribute
    $this.attr('src', id);
  },
  mouseleave: function() {
    var temp = $(this).data('temp'); // lookup temp
    $(this).attr('src', temp); // swap image back
  }
})


/*

var $ = function(id) {
  return document.getElementById(id);
};

function MouseRollover(img) {
  alert("made it");
  var oldIMG = $(this).attr("src");
  var newIMG = $(this).attr("id");
};

window.onload = function() {
    //preload images
    var links = document.getElementsByTagName("li");
    var i, link, image;
    for (i = 0; i < links.length; i++) {
      links = links[i];
      image = new Image();
    }
    //rollover
    $("li").on('mouseenter', function() {
          alert("yep");
        });
        
     }

*/
代码语言:javascript
复制
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<h1>Rollover Test</h1>
<ul id="rollover_test">
  <li>
    <img src="https://encrypted-tbn2.gstatic.com/images?q=tbn:ANd9GcSm2etjS8VnJRwuZA8ormtAyPrIt8x0twLr-APiGwrkcX8NXe3P" alt="Img1" data-id="https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcQ-W40Oxb_QCTaGT9MVgTuXaDxacAKgChfvATaS9KffbHfGc16n">
  </li>
  <li>
    <img src="https://encrypted-tbn2.gstatic.com/images?q=tbn:ANd9GcTp6eZg_pJb0_NFxdaFYSnqMzPMJc-R_iwp2x8HarvdKzoNaCXv" alt="Img2" data-id="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSwvO67upMvk1q3MicNCujQ67D2EgJf8HyVA36FqM9qrv2B4Mue">
  </li>
  <li>
    <img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcREN8xal0JlNdcPcz-94kQqZ8t3uBWEfm3T4LWpPY5PhX7qndGp" alt="Img3" data-id="https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcT07dVJl5ghqji58Su7Gs9RuSuCgleBDUITx2Dngh3ibVWzLfde">
  </li>
</ul>

票数 0
EN

Stack Overflow用户

发布于 2017-02-10 02:40:02

*已解决*

一旦我发现我的javascript没有正确链接,这只是一个玩弄语法的问题。超容易的。我是个白痴。

HTML

代码语言:javascript
复制
        <ul id="image_rollovers">
        <li>
            <img src="images/h1.jpg" alt="images/h4.jpg" id="img1" onmouseenter="MouseEnter('img1');" onmouseout="MouseOut('img1')">
        </li>
        <li>
            <img src="images/h2.jpg" alt="images/h5.jpg" id="img2" onmouseenter="MouseEnter('img2');" onmouseout="MouseOut('img2')">
        </li>
        <li>
            <img src="images/h3.jpg" alt="images/h6.jpg" id="img3" onmouseenter="MouseEnter('img3');" onmouseout="MouseOut('img3')">
        </li> 
    </ul>  

JAVASCRIPT

代码语言:javascript
复制
var $ = function (id) {
return document.getElementById(id);
};

function MouseEnter(id){
var img = $(id);
originalURL = img.src;
var newURL = img.alt;

img.src = newURL;
};

function MouseOut(id){
var img = $(id);

img.src = originalURL;
}

window.onload = function () {
var originalURL;
//preload images
var links = document.getElementsByTagName("li");
var i, link, image;
for (i=0; i<links.length; i++) {
    links = links[i];
    image = new Image();
};
};

就像一种魅力。

谢谢大家的建议和建议。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42149713

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档