简体   繁体   English

jQuery将此作为函数参数传递

[英]Jquery Passing this as function parameter

HTML 的HTML

<input type="button" id="1" class="add" value="+" title="Add" onclick="set(this);"/>

JS JS

function set(obj){
   alert(obj.id);
}

The above code is not working. 上面的代码不起作用。

What I Require 我需要什么

I need to pass the id attribute of the button in the onclick function. 我需要在onclick函数中传递按钮的id属性。 Is there any way I could achieve this. 有什么办法可以实现这一目标。

Your html should be 您的html应该是

 <input type="button" id="1" class="add" value="+" title="Add" onclick="set(this.id);"/>

and write java script function as 并编写Java脚本函数为

function set(id)
   {
   alert(id);
   }

Your title mentions jQuery, but you're working with a JS solution. 您的标题中提到了jQuery,但是您正在使用JS解决方案。 If you want to do this with jQuery: 如果您想使用jQuery:

HTML: HTML:

<input type="button" id="1" class="add" value="+" title="Add" />

JS: JS:

$("#1").click(function(){alert(this.id);});

Fiddle 小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM