jquery - .hover 和 .on 与 jQuery 的问题
问题描述
我在使用 jQuery.hover
并.on
使用 jQuery 进行更改时遇到问题。
$(".nName").hover(function() {
$(this).css({'color': "blue", "text-decoration": "underline"});
},
function() {
$(this).css('color', "black");
});
解决方案
$(".nName").hover(function() {
$(this).css({
'color': "blue",
"text-decoration": "underline"
});
}, function() {
$(this).css('color', "black");
});
setTimeout(function() {
$('body').append('<div class="nName2">Dynamically rendered node1</div>')
// The hover event will take effect here.
/**
$(".nName2").hover(function() {
$(this).css({
'color': "blue",
"text-decoration": "underline"
});
}, function() {
$(this).css('color', "black");
});
**/
}, 2000)
$(".nName2").hover(function() {
$(this).css({
'color': "blue",
"text-decoration": "underline"
});
}, function() {
$(this).css('color', "black");
});
setTimeout(function() {
$('body').append('<div class="nName3">Dynamically rendered node2</div>')
}, 2000)
$('body').on('mouseenter', '.nName3', function() {
$(this).css({
'color': "blue",
"text-decoration": "underline"
});
});
$('body').on('mouseleave', '.nName3', function() {
$(this).css('color', "black");
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="nName">Existing node</div>
初始化页面时,无法将悬停事件绑定到动态呈现的节点。
换句话说,您可以使用mouseenter
andmouseleave
代替hover
.
推荐阅读
- nginx - 在nginx中root几个目录
- linux - 如何在游戏笔记本电脑上安装 linux 发行版?
- python - Python解释器没有出现在Pycharm中
- matlab - 如何在 MATLAB 中将“8.1076e-05”转换为 0.0000811?
- c# - 如何使用 C# 在 Selenium WebDriver 代码中指定 ENTER 按钮功能
- android-viewbinding - 如何修复 CodeLab 上的 ViewBinding 构建错误
- c# - json序列化程序将日期时间转换为任何字符串
- javascript - ASP NET Core (.NET 5.0) 中的脚本部分和缩小文件
- javascript - 确定 Eclipse 是否显示使用 -webkit-line-clamp 截断的文本
- javascript - 即使安装在 node_modules 中,节点也找不到模块