admin管理员组

文章数量:1391784

I want to be able to mouse over a <div> and use JavaScript to get the information (for example the id) and store it in a variable.

What would be the most efficient way to make this happen?

I want to be able to mouse over a <div> and use JavaScript to get the information (for example the id) and store it in a variable.

What would be the most efficient way to make this happen?

Share Improve this question edited Jan 9, 2013 at 9:20 Ionică Bizău 114k94 gold badges310 silver badges487 bronze badges asked Jul 25, 2011 at 23:01 waa1990waa1990 2,4139 gold badges28 silver badges34 bronze badges 3
  • you mean its innerHTML ? – Cystack Commented Jul 25, 2011 at 23:02
  • 2 <div> elements do not have a "value" in normal parlance. What is it about the elements that you want to retrieve? – Pointy Commented Jul 25, 2011 at 23:03
  • what would it set TO? the name/id of the div? something other? – Robot Woods Commented Jul 25, 2011 at 23:03
Add a ment  | 

8 Answers 8

Reset to default 2

This sould do it for you :

$('div').mouseover(function(){
   var content = $(this).html();
   var id = $(this).attr('id');
   alert('the element\'s ID is: '+id+'and content is: '+content);
});
$('#yourDivId').hover(function(){
  var value = $(this).html();
});

You can try this:

var currentDivID;
var currentDivValue;
$(document).ready(function(){

$('div').mouseover(function(){
  currentDivID = this.id
  currentDivValue = $(this).html();
});

});

besides the fact that div's doesn't have values, you can store it's text or argument or something else.

var someVar = '';
$(document).ready(function(){
    $('div').mouseover(function(){
        someVar = $('div').html();
        someVar = $('div').attr('id');
        someVar = $('div').attr('class');
        someVar = $('div').find('input').val();
    }
});
$("div").bind("mouseenter",function(){

var divHTML = $(this).html();

});

here is the fiddle http://jsfiddle/fveRk/

Here is a simple example:

<div id="test">Hello world!</div>

$('#test').mouseover(function(){
    var test = $(this);
    console.log(test);
});

http://jsfiddle/4uLzf/

Attach a mouseover event to the div.

Using jquery:

$('#theDiv').mouseover(function() {
    var value = $(this).html();
    //store value
});

jQuery has a nice way to detect the hovering: http://api.jquery./hover/

When you say "get the value" are you talking about getting the div's contents? In that case, the html() jQuery method will do it.

http://api.jquery./html/

本文标签: javascriptGetting a div39s information when I mouse over itStack Overflow