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
8 Answers
Reset to default 2This 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
版权声明:本文标题:javascript - Getting a div's information when I mouse over it - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744772407a2624418.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论