jQuery - li 元素上的悬停功能

jQuery - Hover function on li element

我有一个列表,我想在鼠标悬停在元素上时更改元素的文本。例如,如果我有这个列表

<ul>
    <li id="first-li">Some text</li>
    <li id="second-li">More text</li>
    <li id="third-li">and more text</li>
</ul>

并且鼠标悬停在第一个元素上,我希望文本更改为例如“第一个元素”。

我正在尝试使用以下代码实现上述目标:

$(document).ready(function(){

    //when mouse is over, save initial value and replace with newText
    function hover_in(newText){
        console.log(newText);
        var $this = $(this);
        $this.data('initialText', $this.text());
        $this.text(newText);
    }

    //when mouse is over, replace with initial value
    function hover_out(){
        var $this = $(this);
        $this.text($this.data('initialText'));
    }

    $('#first-li').hover(function(){hover_in("NEW TEXT")}, hover_out);
});

我可以在控制台打印 hover_in' "NEW TEXT" 的参数中看到值插入,但文本没有改变。

知道我做错了什么吗?

提前致谢!

您的问题是您正在创建两个试图获取 $(this) 变量的函数,该变量仅在 hover 函数中可用。

您必须将 $(this) 作为此函数的变量传递。

$(document).ready(function(){

  //when mouse is over, save initial value and replace with newText
  function hover_in(newText, $this){
    console.log(newText);
    $this.data('initialText', $this.text());
    $this.text(newText);
  }

  //when mouse is over, replace with initial value
  function hover_out($this){
    $this.text($this.data('initialText'));
  }

  $('li').hover(
    function(){
      hover_in("NEW TEXT", $(this));
    }, 
    function() {
      hover_out($(this));
  });
});

因为您是从函数调用 hover_in,所以这不是指 jQuery 元素。您需要在函数中传递它才能引用它。

http://jsfiddle.net/whoacowboy/mguuknny/

JavaScript

$(document).ready(function(){

    //when mouse is over, save initial value and replace with newText
    function hover_in(t,newText){
        t.data('initialText', t.text());
        t.html(newText);
    }

    //when mouse is over, replace with initial value
    function hover_out(){
        var $this = $(this);
        $(this).html($(this).data('initialText'));
    }

    $('#list li').hover(function(){
        hover_in($(this),"NEW TEXT");
    }, hover_out);
});

html

<ul id="list">
    <li> First element </li>
    <li> Second element </li>
</ul>

<!DOCTYPE htmL>
<html>
  <head>
    <title>Test</title>
    <meta charset="utf-8" />
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
    <link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.3/themes/smoothness/jquery-ui.css" />
    <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.3/jquery-ui.min.js"></script>
  </head>
  <body>
    <ul>
      <li id="first">First element</li>
      <li id="second">Second element</li>
    </ul>
  </body>
  <script>
    $(function() {
      var first_maximized = "First element";
      var first_minimized = "1st element";
      var second_maximized = "2nd element";
      var second_minimized = "Second element";
      
      $("#first").hover(
        function() {          
          $("#first").text(first_minimized);          
        },
        function() {
          $("#first").text(first_maximized);
        }
      );
      
      $("#second").hover (
        function() {
          $("#second").text(second_maximized);  
        },
        function() {
          $("#second").text(second_minimized);  
        }
      );
    });
  </script>
</html>

试试这个方法。

我使用了 JQuery 方法 hover,在这种情况下它有两个参数:

  • 首先是鼠标在元素内时的函数
  • 其次是鼠标离开元素时的功能