Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/382.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中,有没有办法在鼠标上方抓取当前项目id?_Javascript_Jquery_Html_Onmouseover - Fatal编程技术网

在javascript中,有没有办法在鼠标上方抓取当前项目id?

在javascript中,有没有办法在鼠标上方抓取当前项目id?,javascript,jquery,html,onmouseover,Javascript,Jquery,Html,Onmouseover,我在javascript中有一个方法,我试图在鼠标上方抓取一个元素的id,有没有办法做到这一点 假设我有一个不带参数的方法 function noArgs() {} 我有两个段落的id是p1和p2,我怎样才能得到悬停段落的id 编辑:这就是我当前获取id的方式,我希望消除方法中的“element”参数 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml

我在javascript中有一个方法,我试图在鼠标上方抓取一个元素的id,有没有办法做到这一点

假设我有一个不带参数的方法

function noArgs() {}
我有两个段落的id是p1和p2,我怎样才能得到悬停段落的id

编辑:这就是我当前获取id的方式,我希望消除方法中的“element”参数

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Test</title>
        <script type="text/javascript" href="js/jquery.js"></script>
        <script type="text/javascript">
            function hoverButton(element) {
                var button = document.getElementById(element);
                switch (button.state) {
                    case "up":
                        button.style.backgroundPosition = "top";
                        button.state = "down";
                        break;
                    default:
                        button.style.backgroundPosition = "bottom";
                        button.state = "up";
                        break;
                }
            }
        </script>
        <style type="text/css">
            .button {
                background-image: url("images/button.png");
                width: 100px;
                height 50px;
                background-position: top;
                border: none;
                font-size: 18px;
            }
        </style>
    </head>

    <body>
        <input type="submit" id="submit_button" class="button" value="Submit" state="up" onmouseover="hoverButton('submit_button')" onmouseout="hoverButton('submit_button')"/>
        <input type="submit" id="submit_button2" class="button" value="Submit" state="up" onmouseover="hoverButton('submit_button2')" onmouseout="hoverButton('submit_button2')"/>
    </body>
</html>

试验
功能悬停按钮(元素){
var按钮=document.getElementById(元素);
开关(按钮状态){
案例“up”:
button.style.backgroundPosition=“top”;
button.state=“down”;
打破
违约:
button.style.backgroundPosition=“bottom”;
button.state=“向上”;
打破
}
}
.按钮{
背景图片:url(“images/button.png”);
宽度:100px;
高度50px;
背景位置:顶部;
边界:无;
字号:18px;
}
使用jQuery:

$element.bind('mouseover', function() {
  var id = $(this).attr('id');
});
使用jQuery:

$element.bind('mouseover', function() {
  var id = $(this).attr('id');
});

为什么要删除元素参数

在您的场景中,您可以执行
onmouseover=“hoverButton(this)”
并省去
var button=document.getElementById(element)函数中的行<然后,code>element
将是当时鼠标移动到的任何元素,您可以通过执行
element.ID
来获取其ID(如果需要)


但是,由于代码中有对jQuery的引用,所以最好使用它;)

为什么要删除元素参数

在您的场景中,您可以执行
onmouseover=“hoverButton(this)”
并省去
var button=document.getElementById(element)函数中的行<然后,code>element
将是当时鼠标移动到的任何元素,您可以通过执行
element.ID
来获取其ID(如果需要)


但是,由于代码中有对jQuery的引用,所以最好使用它;)

请发布您的方法以及如何将其连接到jQuery中的ElementsAsy:)-请发布您的方法以及如何将其连接到jQuery中的ElementsAsy:)-