简体   繁体   English

在没有jQuery的情况下获取jQuery.data功能

[英]Getting jQuery.data functionality without jQuery

You can quite easily set a data attribute of any element with jquery using $('#elid').data('key', value) . 您可以使用$('#elid').data('key', value)用jquery轻松设置任何元素的数据属性。 You can do the same using document.querySelector('#elid').setAttribute('data-key', value) 您可以使用document.querySelector('#elid').setAttribute('data-key', value)

However, jQuery gives you a special ability that querySelector doesn't - the ability to add attributes of an arbitrary type (including functions, and I think promises, which is what I need). 但是,jQuery为您提供了querySelector所没有的特殊功能-能够添加任意类型的属性(包括函数,我认为是我所需要的)。

So if you were to do $('#elid').data('key', function(){console.log('yes')}) with jQuery, and then $('#elid').data('key')() , it would log 'yes' to the console -- we can just assign a function to the element as a data attribute and run it whenever. 因此,如果要使用jQuery执行$('#elid').data('key', function(){console.log('yes')}) ,然后再执行$('#elid').data('key')() ,它将在控制台中记录“是”-我们可以将一个函数作为数据属性分配给元素,并在任何时候运行它。

But we can't do the same with 'setAttribute' -- when we do it, it apparently just assigns a stringified form of the function to the data attribute, rather than the actual function. 但是我们不能对'setAttribute'做同样的事情-当我们这样做时,它显然只是将字符串形式的函数分配给data属性,而不是实际的函数。

I've provided example code here: 我在这里提供了示例代码:

https://jsfiddle.net/8e1wyL41/ https://jsfiddle.net/8e1wyL41/

So how can I apply data to elements with plain javascript, just like jQuery, including the ability to have arbitrary functions or javascript objects as data attribute values? 那么,如何像jQuery一样将数据应用到使用普通JavaScript的元素上,包括将任意函数或JavaScript对象作为数据属性值的功能呢?

jQuery#data() uses an internal object to keep track of data values. jQuery#data()使用一个内部对象来跟踪数据值。 It does not update the element to have new or changed data-* attributes when setting data values. 设置数据值时,它不会将元素更新为具有新的或更改的data- *属性。 When retrieving a data value, if the internal object does not have a set value it will attempt to get it from the data-* attributes. 检索数据值时,如果内部对象没有设置值,它将尝试从data- *属性获取它。

A overly simplified way of doing this without jQuery would be to just use an object and store your data on that 在没有jQuery的情况下,这样做的一种过度简化的方法是仅使用一个对象并将数据存储在该对象上

var element = document.querySelector("div");
element.customData = {};
//get data example, check if customData has a value first, if not use dataset
var someData = element.customData["somedata"] || element.dataset["somedata"];
//set
element.customData["somedata"] = function(){};

If you don't want to contaminate the element with arbitrary properties you could use a WeakMap , pending on browser support, to associate a data object with the element. 如果您不想用任意属性污染元素,则​​可以使用WeakMap (等待浏览器支持)将数据对象与元素关联。 This also allows for using a single object to maintain other element data objects as well. 这也允许使用单个对象来维护其他元素数据对象。 The key to the data object is the element object itself. 数据对象的关键是元素对象本身。 And the data object will get deleted from the map automatically once the element is garbage collected 一旦元素被垃圾回收,数据对象将自动从地图中删除。

var dataMap = new WeakMap();
var element = document.querySelector('div');
var elementData = dataMap.get(element);

if(!elementData){
   dataMap.set(element, elementData = {});
}
//get data example, check if data object has a value first, if not use dataset
var someData = elementData["somedata"] || element.dataset["somedata"];
//set
elementData["somedata"] = function(){};

.dataset sets or gets a DOMString of HTML data-* , though you can use Function() to call the function stored as string at HTMLElement.dataset .dataset设置或获取HTML data-*DOMString ,尽管您可以使用Function()调用以字符串形式存储在HTMLElement.dataset的函数

 document.documentElement.dataset.fn = function fn(...args) {console.log(args)}; new Function("return " + document.documentElement.dataset.fn)()("yes"); 

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

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