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

发布于 2024-07-18 02:21:06 字数 281 浏览 3 评论 0原文

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

<script type="text/javascript">

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

</script>

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

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?

<script type="text/javascript">

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

</script>

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

紫南 2024-07-25 02:21:06

有更简单的方法可以使用 jQuery 来完成此操作,但如果没有它,这是如何完成的:

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

对于本示例,我将事件附加到页面中的所有锚标记,但您可以将特定锚标记的 getElementsByTagName 替换为 getElementById

<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>

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

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.

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.

<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>
栩栩如生 2024-07-25 02:21:06

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

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

As Jose said, it would be easy with jQuery.

$(document).ready(function()
{
    $("a").click(function()
    {
        // this == the clicked element
    });
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文