javascript - jQuery camel-case mapping from "data-" attribute names to ".data()" keys -
If you add a "data-" attribute on an element:
Div id = 'x' data-key = 'value' & gt; Then you can get the value through the jQuery ".data ()" method:
Warning ($ ('$ x'). Data ('key')); // Alert "value" The library uses constant camel-case converter for attribute names with embedded dashes:
& lt; Div id = 'x' data-hello-world = "hi" & gt; Warning ($ ('# x') data ("HelloWorld").); Camel-case converter is a jQuery "global" function:
Warning ($ .aum cass ("hello-world"); // Alert "Hello World" Although this is all broken when the attribute name contains the name of the single letter surrounded by a dash: & Lt; Div id = 'x' data-image-x-offset = '50px '& gt; Warning ($ ('# x') data ('imageXOffset').); // Undefined This is a bit weird, because:
Warning ($ .im image-x-offset '); // "imageXOffset" So what's wrong? I think this is something that has something to do with the mechanism used to go in the Other direction, which was already converting the name of the camel-case back into a dashed shape. I can not even despise it in code though. 1.6.3 seems to be equal to 1.6.3. (By the way, "image-x-offset" can be used to get data.)
Edit - If, for a given element, you Before trying the form of a camel-case, the first dashed form, then it works (and it tells me that it is definitely one: -)
You are right, Rege is used to convert this issue with that camera Reckon with S. To dash from cas
rmultiDash = / ([a-z]) ([A-Z]) / g; ...:
var name = "data-" + key.replace (rmultiDash, "$ 1- $ 2") .toLowerCase ( ); ... resulting in:
Data-Image-Exofet ... instead: < / P>
data-image-x-offset demo: So when you use debash The version, when it looks for a feature, does not get any requirement for the conversion, and then adds the Omal Cas version to the data of elements in jQuery.cache . The subsequent efforts will work because the right camel is now, so now it is not tried to get it as a specialty, and hence no mistake is required for regex.
Comments
Post a Comment