Do HTML5 custom data attributes “work” in IE 6?

后端 未结 6 1813
小鲜肉
小鲜肉 2020-11-22 06:35

Custom data attributes: http://dev.w3.org/html5/spec/Overview.html#embedding-custom-non-visible-data

When I say “work”, I mean, if I’ve got HTML like this:



        
6条回答
  •  感情败类
    2020-11-22 06:41

    Yes, they work.

    IE has supported getAttribute() from IE4 which is what jQuery uses internally for data().

    data = elem.getAttribute( "data-" + key ); // Line 1606, jQuery.1.5.2.js
    

    So you can either use jQuery's .data() method or plain vanilla JavaScript:

    Sample HTML

    Javascript

    var el = document.getElementById("some-data");
    var name = el.getAttribute("data-name");
    alert(name);
    

    jQuery

    var name = $("#some-data").data("name");
    

提交回复
热议问题