SlideShare ist ein Scribd-Unternehmen logo
1 von 15
Downloaden Sie, um offline zu lesen
jQuery .data()
und HTML5 data Attribute
Michael Kühnel
•

Macht Internet seit Netscape 4.7

•

Frontend Developer bei der SMA Solar Technology
AG

•

Twitter: @mkuehnel

•

Website: www.michael-kuehnel.de
Was ist Query .data()?
•

.data() und .removeData() – seit jQuery 1.2.3

•

hat nichts mit HTML5 zu tun

•

Key-Value-Speicher an DOM Elementen
Nutzung kurz erklärt
$('#elem').data( 'foo', 52 );
$('#elem').data( 'bar', { myType: 'test', count: 40 } );
$('#elem').data( { baz: [ 1, 2, 3 ] } );

$('#elem').data( 'foo' ); // 52
$('#elem').data(); // {
foo: 52,
bar: { myType: 'test', count: 40 },
baz: [ 1, 2, 3 ]
}
$('#elem').removeData('foo');
$('#elem').removeData();
Für was kann man das
gebrauchen?
Nutzereingaben zwischenspeichern, Status von
Elementen speichern, IDs aus Datenbanken … usw.
Was sind HTML5 data
Attribute?
Custom Attribute in HTML-Markup (data-*)
<ul>
<li data-foo="52">Element</li>
<li data-foo-bar="{ myType: 'test', count: 40 }">
Element
</li>
</ul>

Empfehlung: Hyphens wegen Namespacing (datamodulname-key).
Browsersupport
HTML-Markup:!
•

Jeder Browser der den HTML5 Doctype versteht


Natives JavaScript:!
•

Zugriff über element.dataset property ab IE11

•

Zugriff über element.getAttribute ab IE5.5
Data Attribute und CSS Attribut
Selektoren
Nur lowercase und Bindestriche verwenden
#element[data-fooBar="25"] {
background-position: -16px 0;
}

💩

#element[data-foo-bar="25"],
#element[data-foobar="25"] {
background-position: -16px 0;
}

👍
jQuery 💖 HTML5 data-*
Data Attribute lesen
HTML-Markup:
<div id="element" data-foo-bar="25">Element</div>

JavaScript:
var data = $('#element').data('fooBar');

Siehe: http://api.jquery.com/data/#data-html5
Datentypen-Konvertierung
mit .data()
HTML-Markup:
<div id="element" data-foo-bar="25">Element</div>
JavaScript:
// 25 -> string
var data = $('#element').attr('data-foo-bar');
!

// 25 -> number
var data = $('#element').data('fooBar');

.data() »kennt« diese Datentypen:
•

booleans, strings, numbers, objects, arrays, null
Beachtenswert I
.data() liest den Value des HTML5 data Attributes nur
beim ersten Aufruf auf dem Element!
HTML-Markup:
<div id="element" data-foo-bar="25">Element</div>
JavaScript:
$('#element').data('fooBar'); // -> 25
$('#element').attr('data-foo-bar', '50');
$('#element').data('fooBar'); // -> 25

Lösung:!
Entweder mit .attr() lesen oder mit .data() setzen.
Beachtenswert II
Zu viel Magie?
HTML-Markup:
<div data-tweet-id="4238802317582131"></div>
<div data-tweet-id="423880231758213120"></div>
JavaScript:
$('div').each(function() {
console.log(typeof($(this).data('tweetId')));
});
// --> number
// --> string // WTF?!?

Grund: Der Maximalwert einer JavaScript Zahl ist 253
– also 9007199254740992
Fragen?!

💭

Twitter: @mkuehnel
E-Mail: mail@michael-kuehnel.de
Links

•

jQuery data im Browser sichtbar machen
•

Chrome: https://chrome.google.com/webstore/
detail/jquery-debugger/
dbhhnnnpaeobfddmlalhnehgclcmjimi

•

Firebug: http://firequery.binaryage.com/
Quellen
•

http://api.jquery.com/data/

•

http://api.jquery.com/removeData/

•

http://www.youtube.com/watch?v=9anCUpoiFVw&feature=youtu.be&t=19m46s

•

http://www.learningjquery.com/2011/09/using-jquerys-data-apis/

•

http://html5doctor.com/html5-custom-data-attributes/

•

http://blog.jquery.com/2008/02/07/jquery-1-2-3-released/

•

http://caniuse.com/#feat=dataset

•

http://thinkco.de/jquery-data-considered-potentially-harmful/

•

http://lookfirst.com/2011/12/dont-use-jquery-data-method-use-attr.html

Weitere ähnliche Inhalte

Empfohlen

How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at WorkGetSmarter
 

Empfohlen (20)

How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 

jQuery .data() und HTML5 data Attribute