JS使用jsBarcode生成條形碼(一維碼)簡單實例
一、安裝
script 引入
<script src="JsBarcode.all.min.js"></script>
地址:https://cdn.jsdelivr.net/npm/jsbarcode@3.11.5/dist/JsBarcode.all.min.js
也可以進(jìn)官網(wǎng)查看地址。
npm方式
安裝:
npm install jsbarcode --save
頁面引入:
import JsBarcode from "jsbarcode";
二、使用
HTML部分加入svg容器
<svg id="barcode"></svg>
JS 代碼部分
JsBarcode("#barcode", "Hi world!");三、結(jié)果

參數(shù)設(shè)置(options)
| option | 默認(rèn)值 | 類型 | 說明 |
|---|---|---|---|
| format | “auto” (CODE128) | String | 條形碼的類型 |
| width | 2 | Number | 每個條條的寬度,注意這里不是指整個條形碼的寬度 |
| height | 100 | Number | 整個條形碼的寬度 |
| displayValue | true | Boolean | 是否顯示條形碼下面的文字 |
| fontOptions | “” | String | 設(shè)置條形碼文本的粗體和斜體樣式 bold / italic / bold italic |
| font | “monospace” | String | 設(shè)置條形碼顯示文本的字體 |
| textAlign | “center” | String | 條形碼文本的水平對齊方式,和css中的類似: left / center / right |
| textPosition | “bottom” | String | 條形碼文本的位置 bottom / top |
| textMargin | 2 | Number | 條形碼文本 和 條形碼之間的間隙大小 |
| fontSize | 20 | Number | 設(shè)置條形碼文本的字體大小 |
| background | “#ffffff” | String (CSS color) | 整個條形碼容器的背景顏色 |
| lineColor | “#000000” | String (CSS color) | 條形碼和文本的顏色 |
| margin | 10 | Number | 整個條形碼的外面距 |
| marginTop | undefined | Number | 整個條形碼的上邊距 |
| marginBottom | undefined | Number | 整個條形碼的下邊距 |
| marginLeft | undefined | Number | 整個條形碼的左邊距 |
| marginRight | undefined | Number | 整個條形碼的右邊距 |
| valid | function(valid){} | Function | 執(zhí)行完條形碼的一個回調(diào)函數(shù),正確true 錯誤false |
options 使用方法
let options = {
fontSize: 12,
background: "#cccccc"
};
JsBarcode("#barcode", "Hi world!", options);
附:JsBarcode - 生成條形碼并打印出來
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script src="js/jquery-1.10.2.min.js"></script>
<script src="js/JsBarcode.all.min.js"></script>
<script src="js/jQuery.print.js"></script>
</head>
<body>
<input id="code" type="text" placeholder="請輸入條形碼"><button id="save">生成條形碼</button>
<br>
<div id="PrintSection">
<img id="test">
</div>
<br>
<button id="print">打印</button>
<script>
$(function () {
// 生成條形碼
$("#save").click(function () {
var requestNo = $("#code").val();
if (requestNo == "") {
return false;
} else {
$("#test").JsBarcode(requestNo);
}
})
// 打印條形碼
$("#print").click(function () {
$("#PrintSection").print({
globalStyles: true,
mediaPrint: false,
stylesheet: null,
noPrintSelector: ".btncontainer",
iframe: true,
append: null,
prepend: null,
manuallyCopyFormValues: true,
deferred: $.Deferred(),
timeout: 750,
title: null,
doctype: '<!doctype html>'
});
})
})
</script>
</body>
</html>
總結(jié)
到此這篇關(guān)于JS使用jsBarcode生成條形碼(一維碼)的文章就介紹到這了,更多相關(guān)jsBarcode生成條形碼內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
需靈活掌握的Bootstrap預(yù)定義排版類 你精通嗎?
Bootstrap預(yù)定義排版類,做web前端開發(fā)的你精通嗎?bootstrap前端框架到底為我們預(yù)定義了那些排版的類呢?感興趣的小伙伴們可以參考一下2016-06-06
elementui的select實現(xiàn)多選添加功能
這篇文章主要介紹了elementui的select實現(xiàn)多選添加功能,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-03-03
JS實現(xiàn)的四級密碼強(qiáng)度檢測功能示例
這篇文章主要介紹了JS實現(xiàn)的四級密碼強(qiáng)度檢測功能,具有實時檢測輸入密碼強(qiáng)度的功能,涉及javascript針對字符串的正則判定相關(guān)操作技巧,需要的朋友可以參考下2017-05-05
Bootstrap基本插件學(xué)習(xí)筆記之按鈕(21)
這篇文章主要為大家詳細(xì)介紹了Bootstrap基本插件學(xué)習(xí)筆記之按鈕的相關(guān)資料,實現(xiàn)按鈕狀態(tài)控制等形式的交互,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-12-12
window.location.href中url中數(shù)據(jù)量太大時的解決方法
這篇文章主要為大家介紹下window.location.href中url中數(shù)據(jù)量太大時的解決方法,需要的朋友可以參考下2013-12-12
javascript HTMLEncode HTMLDecode的完整實例(兼容ie和火狐)
用的瀏覽器內(nèi)部轉(zhuǎn)換器實現(xiàn)轉(zhuǎn)換,方法是動態(tài)創(chuàng)建一個容器標(biāo)簽元素2009-06-06

