jQuery String Template Format Function

https://www.sitepoint.com/jquery-string-template-format-function/

Pretty useful jQuery function I’ve called “formatVarString”. Its takes a string as the first argument with n arguments after with which to perform variable substitution (returning variables as part of a string using parenthesis).

You can simply use {1}, {2}, {3} etc to reference variables in a string.

Usage

formatVarString('we love {1}.', 'jQuery4u');
//output: "we love jQuery4u."

formatVarString('{1} is a {2} aimed to help you learn {3}.', 'jQuery4u', 'blog', 'jQuery');
//output: "jQuery4u is a blog aimed to help you learn jQuery."
The jQuery Format Function

源码:

var JQUERY4U = {};
JQUERY4U.UTIL = {
formatVarString: function()
    {
        var args = [].slice.call(arguments);
        if(this.toString() != '[object Object]')
        {
            args.unshift(this.toString());
        }

        var pattern = new RegExp('{([1-' + args.length + '])}','g');
        return String(args[0]).replace(pattern, function(match, index) { return args[index]; });
    }
}

使用:

JQUERY4U.UTIL.formatVarString('{1} is a {2} aimed to help you learn {3}.', 'jQuery4u', 'blog', 'jQuery');
//output: "jQuery4u is a blog aimed to help you learn jQuery."

Was this helpful?
More:
javascript return string variables {1}

要通过POST请求将matplotlib画出的图传输到网页上显示,并可以从网页上获取键值对,可以使用Python的Flask框架和JavaScript的AJAX技术。具体步骤如下: 1. 安装Flask和matplotlib库 ```python pip install Flask matplotlib ``` 2. 编写Flask应用程序 ```python from flask import Flask, render_template, Response, request import matplotlib.pyplot as plt import io import json app = Flask(__name__) @app.route('/') def index(): return render_template('index.html') @app.route('/plot', methods=['POST']) def plot(): # 从POST请求中获取键值对 kv = request.form.to_dict() # 将键值对转换为x和y的列表 x = [] y = [] for key in sorted(kv.keys()): x.append(key) y.append(int(kv[key])) # 生成一张图片 fig = plt.figure() plt.plot(x, y) # 将图片保存至内存中 buf = io.BytesIO() fig.savefig(buf, format='png') buf.seek(0) # 将图片转换为Flask Response对象 return Response(buf, mimetype='image/png') if __name__ == '__main__': app.run(debug=True) ``` 3. 编写网页模板 ```html <!DOCTYPE html> <html> <head> <title>Matplotlib Plot</title> <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script> <script> $(document).ready(function(){ $('#plot-form').on('submit', function(event){ event.preventDefault(); var kv = {}; $('#plot-form input[type="number"]').each(function(){ kv[$(this).attr('name')] = $(this).val(); }); $.ajax({ url: '/plot', type: 'POST', data: kv, success: function(data){ $('#plot').attr('src', 'data:image/png;base64,' + btoa(String.fromCharCode.apply(null, data))); } }); }); }); </script> </head> <body> <form id="plot-form"> <label for="x1">X1:</label> <input type="number" id="x1" name="1"><br> <label for="x2">X2:</label> <input type="number" id="x2" name="2"><br> <label for="x3">X3:</label> <input type="number" id="x3" name="3"><br> <button type="submit">Plot</button> </form> <br> <img id="plot" src="" alt="Matplotlib Plot"> </body> </html> ``` 4. 运行Flask应用程序 ```python python app.py ``` 5. 在浏览器中打开http://localhost:5000,即可看到一个表单,输入X的值后点击Plot按钮,就可以在下方看到matplotlib画出的图像。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值