Script custom property transfer configuration parameters

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.