Javascript:如何使控件以一种方法发送自身 [英] Javascript: How to make a Control send itself in a method

查看:58
本文介绍了Javascript:如何使控件以一种方法发送自身的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<a href="" id="someId" onclick="SomeMethod(self);"></a>

SomeMethod可能具有的位置:

Where SomeMethod could have:

function SomeMethod(item)
{
  item.setAttribute('name', item.id);
}

代替:

function SomeMethod(itemId)
{
  var someItem;

  someItem = document.getElementById(itemId);
  someItem .setAttribute('name', someItem .id);

}

一个愚蠢的例子,但想法不是发送id本身,而是实际调用该方法的控件.我发誓可以这样做,但是没有运气...部分原因是我什至不知道要搜索什么.

Silly example, but the idea is not to send in the id itself, but the actual control calling the method. I swear this can be done but have had no luck searching... partially because I'm not even sure what to search on.

我以为那是自我,但运行脚本时,自我似乎并不是我想要的.

I thought it was self, but self doesn't seem to be what I want when the script I have runs.

推荐答案

使用this关键字.

<a href="" id="someId" onclick="SomeMethod(this);"></a>

这篇关于Javascript:如何使控件以一种方法发送自身的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆