如何从名为的函数中引用包含事件的元素?

时间:2022-12-07 22:07:30

From inside of the fireAlert() function, how can I reference the element that called it, without having to pass this as an argument through the link?

从fireAlert()函数内部,我如何引用调用它的元素,而不必通过链接将其作为参数传递?

<script type="text/javascript">

//function
function fireAlert(){
    alert();
}

</script>

<a href="javascript:;" onclick="fireAlert();">Fire</a>

2 个解决方案

#1


There are easier ways of doing this with jQuery,but this is how it's done without it:

使用jQuery有更简单的方法可以做到这一点,但这就是没有它的方式:

UPDATE: Please try the following HTML/Javascript code. Notice how I am attaching the onclick event inside of window.onload instead of specifying it as an attribute at the element.

更新:请尝试以下HTML / Javascript代码。请注意我是如何在window.onload中附加onclick事件而不是将其指定为元素的属性。

For this example, I'm attaching the event to all anchor tags in the page, but you could replace getElementsByTagName with getElementById for a specific one.

对于此示例,我将事件附加到页面中的所有锚标记,但您可以使用getElementById替换特定的getElementByTagName。

<html>
    <head>
        <script language="javascript">
        function fireAlert(e)
        {
            var whoCalled = e ? e.target : event.srcElement;

            alert(whoCalled.innerHTML);
        }
        window.onload=function()
        {
            var allHrefs = document.getElementsByTagName("a");
            for(i=0;i<allHrefs.length;i++)
            {
                allHrefs[i].onclick=fireAlert;
            }
        }
        </script>
    </head>
    <body>
        <a href="#">Hello #1</a>
        <a href="#">Hello #2</a>
        <a href="#">Hello #3</a>
        <a href="#">Hello #4</a>        
    </body>
</html>

#2


As Jose said, it would be easy with jQuery.

正如何塞所说,使用jQuery会很容易。

$(document).ready(function()
{
    $("a").click(function()
    {
        // this == the clicked element
    });
});

#1


There are easier ways of doing this with jQuery,but this is how it's done without it:

使用jQuery有更简单的方法可以做到这一点,但这就是没有它的方式:

UPDATE: Please try the following HTML/Javascript code. Notice how I am attaching the onclick event inside of window.onload instead of specifying it as an attribute at the element.

更新:请尝试以下HTML / Javascript代码。请注意我是如何在window.onload中附加onclick事件而不是将其指定为元素的属性。

For this example, I'm attaching the event to all anchor tags in the page, but you could replace getElementsByTagName with getElementById for a specific one.

对于此示例,我将事件附加到页面中的所有锚标记,但您可以使用getElementById替换特定的getElementByTagName。

<html>
    <head>
        <script language="javascript">
        function fireAlert(e)
        {
            var whoCalled = e ? e.target : event.srcElement;

            alert(whoCalled.innerHTML);
        }
        window.onload=function()
        {
            var allHrefs = document.getElementsByTagName("a");
            for(i=0;i<allHrefs.length;i++)
            {
                allHrefs[i].onclick=fireAlert;
            }
        }
        </script>
    </head>
    <body>
        <a href="#">Hello #1</a>
        <a href="#">Hello #2</a>
        <a href="#">Hello #3</a>
        <a href="#">Hello #4</a>        
    </body>
</html>

#2


As Jose said, it would be easy with jQuery.

正如何塞所说,使用jQuery会很容易。

$(document).ready(function()
{
    $("a").click(function()
    {
        // this == the clicked element
    });
});