具有类似类的占位符的jquery

具有类似类的占位符的jquery,jquery,Jquery,我正在尝试使用jquery和css实现占位符 我有两个类“.txt”和“.placeholder”,但我将这个类用于多个文本框。我需要在mouseover事件上执行jquery操作(隐藏占位符) HTML: 请看一下小提琴: 提前感谢。您可以使用来实现这一点 $('.txt').hover(function(){ $(this).prev('div.placeholder').hide(); }, function(){ $(this).prev('div.placeholder

我正在尝试使用jquery和css实现占位符

我有两个类“.txt”和“.placeholder”,但我将这个类用于多个文本框。我需要在mouseover事件上执行jquery操作(隐藏占位符)

HTML:

请看一下小提琴:

提前感谢。

您可以使用来实现这一点

$('.txt').hover(function(){
    $(this).prev('div.placeholder').hide();
}, function(){
    $(this).prev('div.placeholder').show();
})
演示:

正如lee_mcmullen所建议的,传递给
prev
的选择器
'div.placeholder'
是可选的

$('.txt').hover(function(){
    $(this).prev().hide();
}, function(){
    $(this).prev().show();
})
演示:

您可以使用来实现这一点

$('.txt').hover(function(){
    $(this).prev('div.placeholder').hide();
}, function(){
    $(this).prev('div.placeholder').show();
})
演示:

正如lee_mcmullen所建议的,传递给
prev
的选择器
'div.placeholder'
是可选的

$('.txt').hover(function(){
    $(this).prev().hide();
}, function(){
    $(this).prev().show();
})

演示:

您想要在鼠标悬停事件上放置占位符的操作是什么?您想要在鼠标悬停事件上放置占位符的操作是什么?在调用
prev()
as prev()时,不一定需要选择器,因为prev()只获取前一个元素。@lee\u mcmullen yes,但是为了更安全,不一定需要调用
prev()
中的选择器,因为prev()只获取前一个元素。@lee_mcmullen是的,但是为了更安全
$('.txt').hover(function(){
  $(this).prev().hide();
},
function(){
  $(this).prev().show();
})