[英]How to show the id div when hover
I have sevral div,when I hover on one of them I want to show the id on the div 我有sevral div,当我将鼠标悬停在其中一个上时,我想在div上显示id
<div id="display_id_div" >hover me</div>
Thanks. 谢谢。 my html
我的HTML
<div id="display_id_div" >hover me</div>
<div id="div1" >hover me</div>
<div id="div2" >hover me</div>
<div id="div3" >hover me</div>
. 。 jquery code:
jQuery代码:
$(div).hover(function(){
$("#display_id_div") = $(this.id);
})
You are confusing function and assignment, you can't assign something to a function call. 您会混淆函数和赋值,无法将某些赋值给函数调用。
You got several errors in your code: 您的代码中有几个错误:
$(div).hover(function(){
What is div
? 什么是
div
? a variable or? 变量还是? If you mean to select
div
s in your html you need to do: 如果要在html中选择
div
,则需要执行以下操作:
$('div[id^=div]')
The [id^=div]
part is to select div
s with id starting with div
[id^=div]
部分是选择以div
开头的id的div
so now your code looks like 所以现在你的代码看起来像
$('div[id^=div]').hover(function() {
$("#display_id_div") = $(this.id);
});
You need to modify the innerHTML
of #display_id_div
, so you use 您需要修改
#display_id_div
的innerHTML
,因此您可以使用
$('#display_id_div').html(this.id);
If you notice, there are two ways of using .html()
, as a setter and getter. 如果您注意到了,有两种方法可以使用
.html()
作为设置器和获取器。 It acts here as a setter, given an argument. 在给定参数的情况下,它在这里充当设置者。 If called without an argument, it's a getter
如果不带参数调用,那是一个吸气剂
Final code: 最终代码:
$('div[id^=div]').hover(function() {
$('#display_id_div').html(this.id);
});
demo http://jsfiddle.net/jJRJT/4/ or http://jsfiddle.net/jJRJT/5/ 演示 http://jsfiddle.net/jJRJT/4/或http://jsfiddle.net/jJRJT/5/
code 码
$("div").hover(function(){
alert(this.id);
});
or 要么
to put it in html 放在html中
$("#display_id_div").html(this.id);
$('div').hover(function(){ // attach hover callback to all divs.
$('#display_id_div').html(this.id); // change display_id_div innerHTML value
// to the hovered id.
});
just write this line of code you need to make use of .html()
function jquery 只需编写此行代码,您就需要使用
.html()
函数jquery
$("#display_id_div").html( $(this).attr('id'));
you final code will be 您的最终代码将是
$("div").hover(function()
{
$("#display_id_div").html( $(this).attr('id'));
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.