Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/472.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 当我把鼠标移到一个div上时获取它的信息_Javascript_Jquery_Html - Fatal编程技术网

Javascript 当我把鼠标移到一个div上时获取它的信息

Javascript 当我把鼠标移到一个div上时获取它的信息,javascript,jquery,html,Javascript,Jquery,Html,我希望能够将鼠标悬停在一个变量上,并使用JavaScript获取信息,例如id,然后将其存储在一个变量中 实现这一点最有效的方法是什么?您可以尝试以下方法: var currentDivID; var currentDivValue; $(document).ready(function(){ $('div').mouseover(function(){ currentDivID = this.id currentDivValue = $(this).html(); }); });

我希望能够将鼠标悬停在一个变量上,并使用JavaScript获取信息,例如id,然后将其存储在一个变量中

实现这一点最有效的方法是什么?

您可以尝试以下方法:

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

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

});

除了div没有值之外,您还可以存储它的文本、参数或其他内容

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 id="test">Hello world!</div>

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

将鼠标悬停事件附加到div

使用jquery:

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

jQuery有一种很好的检测悬停的方法:

当你说获取值时,你是说获取div的内容吗?在这种情况下,html jQuery方法将完成这项工作


这可以帮你做到:

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

你是说它的内部HTML?按正常说法,元素没有值。您想要检索的元素是什么?它将设置为什么?div的名称/id?还有别的吗?
$('#yourDivId').hover(function(){
  var value = $(this).html();
});
$('div').mouseover(function(){
   var content = $(this).html();
   var id = $(this).attr('id');
   alert('the element\'s ID is: '+id+'and content is: '+content);
});