Search

11/16/2012

HTML5 APIs You Didn't Know Existed

HTML5 APIs You Didn't Know Existed Element.classList

// Add a class to an element
myElement.classList.add("newClass");

// Remove a class to an element
myElement.classList.remove("existingClass");

// Check for existence
myElement.classList.contains("oneClass");

// Toggle a class
myElement.classList.toggle("anotherClass");
ContextMenu API The new ContextMenu API is excellent: instead of overriding the browser context menu, the new ContextMenu API allows you to simply add items to the browser's context menu:
Element.dataset The dataset API allows developers to get and set data- attribute values
/*  Assuming element:

  
*/ // Get the element var element = document.getElementById("myDiv"); // Get the id var id = element.dataset.id; // Retrieves "data-my-custom-key" var customKey = element.dataset.myCustomKey; // Sets the value to something else element.dataset.myCustomKey = "Some other value"; // Element becomes: //

沒有留言: