Introduction to the use of data custom attributes in HTML and plug-in applications

Introduction to the use of data custom attributes in HTML and plug-in applications
You may often see some HTML with data attributes. These are custom attributes of HTML5. They can do a lot of things and are very convenient to call JS directly. Although they are attributes of HTML5, fortunately, jQuery is universal, so they can be used normally in basically all browsers, including lower versions of IE. Here is a brief introduction to how to use it:
1. Easy to use

Copy code
The code is as follows:

<div id="widget" data-text="123456"></div>


Copy code
The code is as follows:

$(function(){
var _widget = $("#widget").attr("data-text"); alert(_widget); //Because data-text="123456", it prints out 123456
})

2. Use with $.fn.extend to write plugins

Copy code
The code is as follows:

<div id="widget" data-widget-config="{effect:'click'}">This is the test area</div>


Copy code
The code is as follows:

//Plugin extension part
;(function($){
$.fn.extend({
Test:function(config){
/**
* @param effect effect
* config||{} When custom properties are passed in, the default value is not executed
*/
// Set default values
config=$.extend({
effect:'click',
},config||{});
var effect = config.effect;
var _text=config._text;
if(effect=='click'){
$(this).click(function(){
alert('this click');
})
}else if(effect=='mouseover'){
$(this).mouseover(function(){
alert("this is mouseover");
})
}
}
})
})(jQuery)


Copy code
The code is as follows:

//Calling part, the data attribute in HTML depends on this
$(function(){
var _widget = $("#widget").attr("data-widget-config");
// There are two ways to convert a string into a json object
var widgetConfigJSON = eval("("+_widget+")");
// var widgetConfigJSON = (new Function("return " + _widget))();
$("#widget").Test(widgetConfigJSON);
//Because the data attribute in HTML is data-widget-config="{effect:'click'}", the click event will be called here.
If data-widget-config="{effect:'mouseover'}", the event of mouse moving over is called})

<<:  Detailed explanation of various methods of Vue component communication

>>:  CSS3 flip card number sample code

Recommend

XHTML tags that are easily confused by the location of the use

<br />We have always emphasized semantics in...

JavaScript custom plug-in to implement tab switching function

This article shares the specific code of JavaScri...

How to implement JavaScript's new operator yourself

Table of contents Constructor new Operator Implem...

Docker installs ClickHouse and initializes data testing

Clickhouse Introduction ClickHouse is a column-or...

Basic usage of custom directives in Vue

Table of contents Preface text 1. Global Registra...

uni-app implements NFC reading function

This article shares the specific code of uni-app ...

Details on overriding prototype methods in JavaScript instance objects

Table of contents In JavaScript , we can usually ...

XHTML no longer uses some obsolete elements in HTML

When we do CSS web page layout, we all know that i...

How to store false or true in MySQL

MySQL Boolean value, stores false or true In shor...

HTML table tag tutorial (20): row background color attribute BGCOLOR

The BGCOLOR attribute can be used to set the back...

Detailed description of the life cycle of React components

Table of contents 1. What is the life cycle 2. Lo...

Convert XHTML CSS pages to printer pages

In the past, creating a printer-friendly version ...

Introduction to the use of MySQL official performance testing tool mysqlslap

Table of contents Introduction Instructions Actua...

Web page creation question: Image file path

This article is original by 123WORDPRESS.COM Ligh...

How to implement nested if method in nginx

Nginx does not support nested if statements, nor ...