jQuery在一个方法上安装相同元素的单击和鼠标悬停事件的事件处理程序。问题是,当我只使用悬浮方法时,它可以工作,但不适用于单击方法。
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>MyInformation</title>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
<p id = "p1">This is paragraph 1</p>
<p id = "p2">This is paragraph 2</p>
<p id = "p3">This is paragraph 3</p>
<script>
$(p1).on
({
mouseenter: function ()
{
$(this).css("color","red");
},
mouseleave: function () {
$(this).css("color","black");
}
click: function () {
alert("hello");
}
});
</script>
</body>
</html>发布于 2018-10-02 05:08:18
您有错误的选择器和一些语法错误,下面是固定版本:
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>MyInformation</title>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
<p id = "p1">This is paragraph 1</p>
<p id = "p2">This is paragraph 2</p>
<p id = "p3">This is paragraph 3</p>
<script>
$('#p1').on
({
mouseenter: function () {
$(this).css("color","red");
},
mouseleave: function () {
$(this).css("color","black");
},
click: function () {
alert("hello");
}
});
</script>
</body>
</html>发布于 2018-10-02 05:10:27
您错过了mouseleave函数后面的逗号
$('#p1').on
({
mouseenter: function () one mouse enter
{
$(this).css("color","red");
},
mouseleave: function () {
$(this).css("color","black");
},
click: function () {
alert("hello");
}
});发布于 2018-10-02 05:18:54
代码中的两个错误
$(p1)应该是$('#p1')https://stackoverflow.com/questions/52602296
复制相似问题