jQuery mouseenter/leave 不适用于 li:nth-child():before
jQuery mouseenter/leave doesn't work with li:nth-child():before
在下面的代码中,我尝试显示内容:text 和 jquery.
如果我删除 :before
就可以了!为什么 jQuery 不允许,而 javascript 却允许。
This post (7 years ago)
谈到了我的问题,但它没有给出简单的解决方案就解决了这个问题。
今天没有针对目标的简单解决方案:before
?
感谢您的帮助
$(document).ready(function() {
$("div").mouseenter(function() {
$("li:nth-child(2):before").show();
});
$("div").mouseleave(function() {
$("li:nth-child(2):before").hide();
});
});
li {
display: none;
}
li:before {
content: 'Show me';
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul>
<li>This is a paragraph.</li>
<li>This is a second paragraph.</li>
</ul>
<div style="width:30px; height:30px; background:black;cursor:pointer;position:absolute; top:50px;"></div>
伪元素的可见性取决于其主要元素 li:nth-child(2)
的可见性(即 display
状态)。所以你必须在你的 jQuery 函数中添加一个相应的行到 show/hide 主要元素:
$(document).ready(function(){
$("div").mouseenter(function(){
$("li:nth-child(2)").show();
$("li:nth-child(2):before").show();
});
$("div").mouseleave(function(){
$("li:nth-child(2)").hide();
$("li:nth-child(2):before").hide();
});
});
li{display:none;}
li:before{content:'Show me';}
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
<ul>
<li>This is a paragraph.</li>
<li>This is a second paragraph.</li>
</ul>
<div style="width:30px; height:30px; background:black;cursor:pointer;position:absolute; top:50px;"></div>
</body>
</html>
在下面的代码中,我尝试显示内容:text 和 jquery.
如果我删除 :before
就可以了!为什么 jQuery 不允许,而 javascript 却允许。
This post (7 years ago) 谈到了我的问题,但它没有给出简单的解决方案就解决了这个问题。
今天没有针对目标的简单解决方案:before
?
感谢您的帮助
$(document).ready(function() {
$("div").mouseenter(function() {
$("li:nth-child(2):before").show();
});
$("div").mouseleave(function() {
$("li:nth-child(2):before").hide();
});
});
li {
display: none;
}
li:before {
content: 'Show me';
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul>
<li>This is a paragraph.</li>
<li>This is a second paragraph.</li>
</ul>
<div style="width:30px; height:30px; background:black;cursor:pointer;position:absolute; top:50px;"></div>
伪元素的可见性取决于其主要元素 li:nth-child(2)
的可见性(即 display
状态)。所以你必须在你的 jQuery 函数中添加一个相应的行到 show/hide 主要元素:
$(document).ready(function(){
$("div").mouseenter(function(){
$("li:nth-child(2)").show();
$("li:nth-child(2):before").show();
});
$("div").mouseleave(function(){
$("li:nth-child(2)").hide();
$("li:nth-child(2):before").hide();
});
});
li{display:none;}
li:before{content:'Show me';}
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
<ul>
<li>This is a paragraph.</li>
<li>This is a second paragraph.</li>
</ul>
<div style="width:30px; height:30px; background:black;cursor:pointer;position:absolute; top:50px;"></div>
</body>
</html>