DOM节点标签自定义属性以及jQuery对它的绑定和读取

    xiaoxiao2021-03-25  5

    html5允许开发者自由为其标签添加属性,这种自定义属性一般用“data-”开头。

    jQuery的.data()方法除了可以获取自己绑定的数据,也可以获取上述 在标签添加的自定义属性对应的数据,请看例子:

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> <script src="jquery-1.8.3.js"></script> <script type="text/javascript"> $(function(){ $("#tag_a").data("apple","red"); var apple = $("#tag_a").data("apple"); var orange = $("#tag_a").data("orange"); console.log(apple); console.log(orange); }); </script> </head> <body> <a id="tag_a" href="#" data-orange="yellow"></a> </body> </html>输出结果:

    转载请注明原文地址: https://ju.6miu.com/read-156049.html

    最新回复(0)