将鼠标悬停在该段落上

时间:2013-02-11 06:07:04

标签: javascript jquery html

我想创建一个段落,如果用户将鼠标悬停在该段落上,它应显示一个警告框。但是我输入的代码不起作用。只要鼠标进入页面,就会显示该框。我只想在鼠标位于段落时显示它。代码是:

<html>
<script src="jquery.js" type="text/javascript"></script>
<script>
$('document').ready(function(){
    $('#p1').hover(
        alert("you have entered p1 .")
    );
});
</script>
<body>
    <p id="p1">hover here!!</p>
</body>
</html>

7 个答案:

答案 0 :(得分:3)

Working example on jsFiddle

试试这个:

$('#p1').hover(
    alert("you have entered p1 .");
);

或:

$('#p1').hover(
    function() {
        alert("you have entered p1 .");
    },
    function() {
        alert('you have exited p1 .');
    }
);

答案 1 :(得分:2)

<html>
<script src="jquery.js" type="text/javascript"></script>
<script>
$('document').ready(function(){
   $('#p1').hover(function(){
       alert("you have entered p1 .")
   });
});
</script>
<body>
<p id="p1">hover here!!</p>
</body>
</html>

你忘记了#

答案 2 :(得分:1)

<script>
$('document').ready(function(){
    $('#p1').hover(
        alert("you have entered p1 .")
    );
});
</script>
<body>
<p id="p1">hover here!!</p>

答案 3 :(得分:1)

您的脚本中有两个错误:

  1. '记录'引号不在那里
  2. 在悬停中缺少功能
  3. 所以它应该是:

    $(document).ready(function(){ // <----removed the quotes
       $('#p1').hover(function(){ //<------added the function here.
          alert("you have entered p1 .")
       });
    });
    

答案 4 :(得分:1)

//试试这个1老兄...

$("#p1").hover(
function () {
   alert("you have entered p1 .")
},
function () {}
);

答案 5 :(得分:0)

<html>
<head>
<title></title>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
<p id="p1">hover here!!</p>
<script type="text/javascript">
$('#p1').mouseover(function() {
  alert("you have entered p1 .");
});
</script>
</body>
</html>

答案 6 :(得分:0)

如果您希望对页面中的所有段落悬停效果,请转到此

$('p').hover(function1(),function2());

(或) 如果你想要特定段落的悬停效果,那么

$('#ID_OF_YOUR_PARAGRAPH').hover(function1(),function2());