简体   繁体   English

我可以确定使用JavaScript单击的内容吗?

[英]Can I determine what was clicked using JavaScript?

Once again I've inherited someone else's system which is a bit of a mess. 我再次继承了别人的系统,这有点混乱。 I'm currently working with an old ASP.NET (VB) webforms app that spits JavaScript onto the client via the server - not nice! 我目前正在使用旧的ASP.NET(VB)Webforms应用程序,该应用程序通过服务器将JavaScript吐到客户端上-不好! I'm also limited on what I can edit in regards to the application. 对于应用程序,我也只能进行编辑。

I have a scenario where I have a function that does a simple exercise but would also need to know what item was clicked to executed the function, as the function can be executed from a number of places within the system... 我遇到的情况是,我有一个函数可以进行简单的练习,但是还需要知道单击了哪个项目才能执行该函数,因为该函数可以在系统中的许多地方执行...

Say I had a function like so... 说我有一个像这样的功能...

 function updateMyDiv() {
    $('#div1').hide();
    $('#div2').hide();
    $('#div13').show();
 }

how could I get the ID (for example) of the HTML element that was clicked to execute this? 我如何获取被单击以执行此操作的HTML元素的ID(例如)?

Something like: 就像是:

function updateMyDiv() {

    alert(htmlelement.id) // need to raise the ID of what was clicked, 

    $('#div1').hide();
    $('#div2').hide();
    $('#div13').show();
}

I can expand on this if neccessary, do I need to pass this as an arguement? 如果需要,我可以对此进行扩展,是否需要将this作为辩论?

The this keyword references the element that fired the event. this关键字引用触发事件的元素。 Either: 或者:

<element onClick="doSomething(this);">

or 要么

element.onclick = function() {
    alert(this.id);
}

Bind your click events with jQuery and then reference $(this) 将您的点击事件与jQuery绑定,然后引用$(this)

$('.myDivClass').live('click', function () {
    updateMyDiv(this);
});

var updateMyDiv = function (that) {
   alert(that.id);

   // save the world
};

You don't need to pass " this ", it is assigned automatically. 您无需传递“ this ”,它会自动分配。 You can do something like this: 您可以执行以下操作:

$('div').click(function(){
  alert($(this).attr('id'));
})

Attach the function as the elements event handler is one way, 将函数作为元素事件处理程序附加是一种方法,

$(htmlelement).click(updateMyDiv);

If you are working with an already generated event, you can call getElementByPoint and pass in the events x,y coords to get the element the mouse was hovering over. 如果要处理已经生成的事件,则可以调用getElementByPoint并传递事件x,y坐标,以获取鼠标悬停的元素。

$('.something').click(function(){
    alert($(this).attr('id'));
});

You would need to pass it the event.target variable. 您需要将event.target变量传递给它。

$("element").click(function(event) {
    updateMyDiv($(event.target));
});

function updateMyDiv(target) {
    alert(target.prop("id"));
}

Where is your .click event handler? 您的.click事件处理程序在哪里? Wherever it is, the variable this inside of it will be the element clicked upon. 无论在哪里,可变this它里面将是元件时点击。

If you have an onclick attribute firing your function, change it to 如果您具有触发功能的onclick属性,请将其更改为

<tag attribute="value" onclick="updateMyDiv(this)">

and change the JavaScript to 并将JavaScript更改为

function updateMyDiv(obj) {
    alert(obj.getAttribute('id')) // need to raise the ID of what was clicked, 

    $('#div1').hide();
    $('#div2').hide();
    $('#div13').show();
}

使用.attr('id')方法并指定将返回所需内容的ID。

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

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