Jquery 悬停元素A,显示/隐藏元素B
我有一个包含图像的Jquery 悬停元素A,显示/隐藏元素B,jquery,hover,Jquery,Hover,我有一个包含图像的元素。在这个div中,我有一个元素,它保存了一些关于图像的信息。我想将包含图像的悬停,然后显示或隐藏元素 <div class="box"> <img src="img/an_039_AN_diskette.jpg" width="310px" height="465px" /> 6 Pharma IT <p class="hoverbox">some information</p> </div>
元素。在这个div中,我有一个
元素,它保存了一些关于图像的信息。我想将包含图像的
悬停,然后显示或隐藏
元素
<div class="box">
<img src="img/an_039_AN_diskette.jpg" width="310px" height="465px" />
6 Pharma IT
<p class="hoverbox">some information</p>
</div>
6医药IT
一些信息
在jQuery中有没有一种简单的方法可以做到这一点
$("css_selector_of_img").hover(
function () {
$("css_selector_of_p_element").show();
},
function () {
$("css_selector_of_p_element").hide();
}
);
请参见以下内容将匹配您的所有图像,并将目标对准其第一个具有标记p的兄弟姐妹
<script type="text/javascript">
$(document).ready(function(){
$("div.box img").hover(
function(){$(this).siblings("p:first").show();},
function(){$(this).siblings("p:first").hide();}
);
});
</script>
<div class="box">
<img src="somefile.jpg" />
<p>This text will toggle.</p>
</div>
$(文档).ready(函数(){
$(“div.box img”)。悬停(
函数(){$(this).sides(“p:first”).show();},
函数(){$(this).sides(“p:first”).hide();}
);
});
此文本将切换
一些文本
$('div.box img')。悬停(
函数(){$('div.box p.hoverbox').show();},
函数(){$('div.box p.hoverbox').hide();}
);
您能提供您想要做的事情的HTML标记吗?我有点不清楚。对不起,我忘了格式化代码。我该把它放在哪里?当我在document.ready部分在脑海中写下这句话时,它只会打断我的js的其余部分。将它放在页面末尾,在前面,用jQuery与使用document并行。ready。别忘了用逗号分隔这两个函数。我从firebug得到这句话:missing;before语句可能是因为两个函数之间缺少逗号。我刚刚试过,它对我有效。不再有错误,但效果仍然不起作用。可能是因为我用includes和一个php开关创建了整个页面,javascript有点崩溃了吗?我也在做同样的事情。。但我的段落大约有100行。因此,我必须向下滚动我的段落以获得完整内容。在这种情况下,当我从A内容中删除悬停时,内容将隐藏在此处(图像)。。我该怎么办
$('#divwithimage').hover(
function(){$('#ptag').show();}, //shows when hovering over
function(){$('#ptag').hide();} //Hides when hovering finished
);
<div class="box">
<img ... />
<p class="hoverbox">Some text</p>
</div>
<script type="text/javascript">
$('div.box img').hover(
function() { $('div.box p.hoverbox').show(); },
function() { $('div.box p.hoverbox').hide(); }
);
</script>