十四、怎么获取data-id中的值

<!DOCTYPE html>  
<html>  
<head lang="en">  
    <meta charset="UTF-8">  
    <title></title>  
    <script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script></script>
</head>  
<body>  
<div id='plant' data-fruit='12'>来呀! </div>
    <script type="text/javascript">  
    $(function(){
        $('#plant').hover(function(){
            var f1 = $(this).data('fruit');
            var f2 = $(this).attr('data-fruit');
        console.log(f1);
        console.log(f2);
        })
    }) 
    </script> 
</body>  

</html>

  

原文地址:https://www.cnblogs.com/deng-jie/p/9144678.html