相当于纯JavaScript的jQuery函数 [英] Jquery functions equivalent in pure javascript

查看:92
本文介绍了相当于纯JavaScript的jQuery函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个包含所有jQuery函数及其浏览器本地提供的API中的等效项的文档.

I'm looking for a doc with all jQuery functions and their equivalents in APIs provided natively by browsers.

有时候我只需要一个特定的jQuery函数(.detach().insertAfter().on(),...),并且为此仅加载jQuery.

Sometime I only need a specific jQuery function (.detach(), .insertAfter(), .on(), ...) and I load jQuery only for this.

我想停止对jQuery的依赖,而将jQuery转换为非库的翻译对我有很大帮助(我认为很多人).

I would like to stop my jQuery dependence, and a jQuery to non-library translator would help me a lot (and many people, I think).

网络上是否有类似的东西?

Is there something like this somewhere on the web?

我找到了它: http://youmightnotneedjquery.com/

推荐答案

仅在获取jQuery对象$(SELECTOR)后才调用这些jQuery函数.如果您说不需要jQuery选择器代码,而只需要带有(可能是)HTML DOM元素的函数,则必须将其从jQuery源代码中剥离(

You only call those jQuery functions after getting the jQuery object $(SELECTOR). If you saying you don't need the jQuery selector code and just want functions that take (perhaps) an HTML DOM element you'd have to rip it from the jQuery source code (http://code.jquery.com/jquery-latest.js), with dependencies and just the size and complexity this can be a painful process.

JS等效项:

.detach-.removeChild

.detach - .removeChild

var par = elm.parentNode;
par.removeChild(elm);

.insertAfter-.insertBefore 如何不用库就可以在JavaScript中插入After()吗?

.insertAfter - .insertBefore How to do insert After() in JavaScript without using a library?

function insertAfter(referenceNode, newNode) {
    referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling);
}

.on-addEventListener/attachEvent

.on - addEventListener / attachEvent

if(elm.addEventListener)
    elm.addEventListener(EVENT_NAME, function() {}, false);
else
    elm.attachEvent('on' + EVENT_NAME, function() {});

现在,如果您想绑定事件,以便处理程序具有对特定对象的THIS引用...

Now if you want to bind events so that the handler has a THIS reference to a specific object...

function bind( scope, fn ) {
    return function () {
        fn.apply( scope, arguments );
    };
}
if(elm.addEventListener)
    elm.addEventListener(EVENT_NAME, bind(SCOPE_OBJECT, function(){}), false);
else
    elm.attachEvent('on' + EVENT_NAME, bind(SCOPE_OBJECT, function(){}));

这篇关于相当于纯JavaScript的jQuery函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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