MCPcopy Create free account
hub / github.com/JsAaron/jQuery / dataAttr

Function dataAttr

2.0.3/Data.js:379–406  ·  view source on GitHub ↗

* 处理HTML5的data-*属性 * 逻辑结构更为清晰 * @param {[type]} elem [description] * @param {[type]} key [description] * @param {[type]} data [description] * @return {[type]} [description]

(elem, key, data)

Source from the content-addressed store, hash-verified

377 * @return {[type]} [description]
378 */
379 function dataAttr(elem, key, data) {
380 var name;
381
382 // If nothing was found internally, try to fetch any
383 // data from the HTML5 data-* attribute
384 if (data === undefined && elem.nodeType === 1) {
385 name = "data-" + key.replace(rmultiDash, "-$1").toLowerCase();
386 data = elem.getAttribute(name);
387
388 if (typeof data === "string") {
389 try {
390 data = data === "true" ? true :
391 data === "false" ? false :
392 data === "null" ? null :
393 // Only convert to a number if it doesn't change the string
394 +data + "" === data ? +data :
395 rbrace.test(data) ? JSON.parse(data) :
396 data;
397 } catch (e) {}
398
399 // Make sure we set the data so it isn't changed later
400 data_user.set(elem, key, data);
401 } else {
402 data = undefined;
403 }
404 }
405 return data;
406 }
407

Callers 1

Data.jsFile · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected