Script custom property transfer configuration parameters
At the beginning of my formal career, I have been working on a unified header js for my company in the last few days. I thought of a method for passing configuration parameters through script custom attributes. Sometimes we have compiled a js plug-in. To use this plug-in, we need to first introduce the plug-in Js in html, then add a script tag, and call it in it. Such as an image switching plug-in. The code is roughly as follows: copy the code $. fn. picSwitch = function (option) {// here is the picture switching code} After copying the Code and introducing this plug-in, you need to add the call code $ ('# pic') to another script tag '). picSwitch ({'speed': '20170101', 'derection ': 'left '//... here is configuration}) Of course there is no problem, but sometimes we don't want to add more script tags. If we only introduce script tags, how can we pass the configuration parameters? At this time, we can use the Custom Attributes on the script to pass the configuration parameters. Before that, you must process the image switching plug-in. The modified code is as follows: copy the code $. fn. picSwitch = function () {// here is the code for image switching}; // After writing the plug-in, call $ directly ('here is the selector, which needs to be obtained on the script tag '). picSwitch ('here is the configuration parameter, which needs to be obtained on the script tag '); copy the code and then pass the parameter on the script. Reference The js plug-in as follows on the html page. Copy the Code