简单的 Javascript 来模仿在事件处理程序中使用 this 的 jQuery 行为

2023-04-19前端开发问题
2

本文介绍了简单的 Javascript 来模仿在事件处理程序中使用 this 的 jQuery 行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

这不是关于 jQuery 的问题,而是关于 jQuery 如何实现这种行为的问题.

This is not a question about jQuery, but about how jQuery implements such a behaviour.

在 jQuery 中你可以这样做:

In jQuery you can do this:

$('#some_link_id').click(function() 
{
   alert(this.tagName); //displays 'A'
})

有人可以笼统地解释一下(无需您编写代码)他们如何获得将事件的调用者 html 元素(此特定示例中的链接)传递到 this 关键字?

could someone explain in general terms (no need you to write code) how do they obtain to pass the event's caller html elments (a link in this specific example) into the this keyword?

我显然试图在 jQuery 代码中查看 1st,但我无法理解一行.

I obviously tried to look 1st in jQuery code, but I could not understand one line.

谢谢!

更新:根据 Anurag 的回答,我决定在这一点上发布一些代码,因为它似乎比我想象的更容易编码:

UPDATE: according to Anurag answer I decided to post some code at this point because it seems easier to code than what I thought:

function AddEvent(html_element, event_name, event_function)
{      
   if(html_element.attachEvent) //IE
      html_element.attachEvent("on" + event_name, function() {event_function.call(html_element);});
   else if(html_element.addEventListener) //FF
      html_element.addEventListener(event_name, event_function, false); //don't need the 'call' trick because in FF everything already works in the right way         
}

然后现在我们通过一个简单的调用来模拟在事件处理程序中使用 this 的 jQuery 行为

and then now with a simple call we mimic jQuery behaviour of using this in events handlers

AddEvent(document.getElementById('some_id'), 'click', function()
{            
   alert(this.tagName); //shows 'A', and it's cross browser: works both IE and FF
}); 

你认为有什么错误或我误解的东西太肤浅了吗?

推荐答案

在 Javascript 中,您可以通过编程方式调用函数并告诉它 this 应该引用什么,然后使用Function 中的 callapply 方法.函数在 Javascript 中也是一个对象.

In Javascript, you can call a function programmatically and tell it what this should refer to, and pass it some arguments using either the call or apply method in Function. Function is an object too in Javascript.

jQuery 遍历其结果中的每个匹配元素,并调用该对象上的 click 函数(在您的示例中)将元素本身作为上下文或 this指的是该函数内部.

jQuery iterates through every matching element in its results, and calls the click function on that object (in your example) passing the element itself as the context or what this refers to inside that function.

例如:

function alertElementText() {
    alert(this.text());
}

这将调用上下文 (this) 对象上的文本函数,并提醒它的值.现在我们可以调用该函数并将上下文 (this) 设置为 jQuery 包装的元素(这样我们就可以直接调用 this 而无需使用 $(this).

This will call the text function on the context (this) object, and alert it's value. Now we can call the function and make the context (this) to be the jQuery wrapped element (so we can call this directly without using $(this).

<a id="someA">some text</a>
alertElementText.call($("#someA")); // should alert "some text"

使用 callapply 调用函数之间的区别是微妙的.call 参数将按原样传递,而 apply 参数将作为数组传递.阅读更多关于 应用 和 调用 MDC.

The distinction between using call or apply to call the function is subtle. With call the arguments will be passed as they are, and with apply they will be passed as an array. Read up more about apply and call on MDC.

同样,当调用 DOM 事件处理程序时,this 已经指向触发事件的元素.jQuery 只是调用你的回调并将上下文设置为元素.

Likewise when a DOM event handler is called, this already points to the element that triggered the event. jQuery just calls your callback and sets the context to the element.

document.getElementById("someId").onclick = function() {
    // this refers to #someId here
}

这篇关于简单的 Javascript 来模仿在事件处理程序中使用 this 的 jQuery 行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

js删除数组中指定元素的5种方法
在JavaScript中,我们有多种方法可以删除数组中的指定元素。以下给出了5种常见的方法并提供了相应的代码示例: 1.使用splice()方法: let array = [0, 1, 2, 3, 4, 5];let index = array.indexOf(2);if (index -1) { array.splice(index, 1);}// array = [0,...
2024-11-22 前端开发问题
182

JavaScript小数运算出现多位的解决办法
在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会...
2024-10-18 前端开发问题
301

JavaScript(js)文件字符串中丢失"\"斜线的解决方法
问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转...
2024-10-17 前端开发问题
437

layui中table列表 增加属性 edit="date",不生效怎么办?
如果你想在 layui 的 table 列表中增加 edit=date 属性但不生效,可能是以下问题导致的: 1. 缺少日期组件的初始化 如果想在表格中使用日期组件,需要在页面中引入 layui 的日期组件,并初始化: script type="text/javascript" src="/layui/layui.js"/scrip...
2024-06-11 前端开发问题
455

Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript
Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)...
2024-04-20 前端开发问题
5

CoffeeScript 总是以匿名函数返回
CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)...
2024-04-20 前端开发问题
13