JS樣式獲取的封裝方法實例詳解
樣式獲取
style屬性 只能獲取標(biāo)簽內(nèi)容style屬性里面存在的一些樣式
如果你需要獲取對應(yīng)的全局所有地方設(shè)置樣式 我們就需要采用一些方法
getComputedStyle 方法屬于window的方法
Window.getComputedStyle()方法返回一個對象,該對象在應(yīng)用活動樣式表并解析這些值可能包含的任何基本計算后報告元素的所有 CSS 屬性的值。
window.getComputedStyle(元素對象,null) //返回給你的是一個樣式對象
ie兼容
element.currentStyle //返回給你一個樣式對象
兼容封裝
//方法的封裝
function getStyle(element,attr){
var style = window.getComputedStyle?window.getComputedStyle(element):element.currentStyle
return style[attr]
}
//調(diào)用
console.log(getStyle(box,'height'));補充:下面看下js封裝常用的方法
constbaseURL=“http://xxxxx”
日期格式化
export function parseTime(time, pattern) {
if (arguments.length === 0 || !time) {
return null
}
const format = pattern || '{y}-{m}-giinjtd {h}:{i}:{s}'
let date
if (typeof time === 'object') {
date = time
} else {
if ((typeof time === 'string') && (/^[0-9]+$/.test(time))) {
time = parseInt(time)
} else if (typeof time === 'string') {
time = time.replace(new RegExp(/-/gm), '/');
}
if ((typeof time === 'number') && (time.toString().length === 10)) {
time = time * 1000
}
date = new Date(time)
}
const formatObj = {
y: date.getFullYear(),
m: date.getMonth() + 1,
d: date.getDate(),
h: date.getHours(),
i: date.getMinutes(),
s: date.getSeconds(),
a: date.getDay()
}
const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
let value = formatObj[key]
// Note: getDay() returns 0 on Sunday
if (key === 'a') {
return ['日', '一', '二', '三', '四', '五', '六'][value]
}
if (result.length > 0 && value < 10) {
value = '0' + value
}
return value || 0
})
return time_str
}表單重置
export function resetForm(refName) {
if (this.$refs[refName]) {
this.$refs[refName].resetFields();
}
}添加日期范圍
export function addDateRange(params, dateRange) {
var search = params;
search.beginTime = "";
search.endTime = "";
if (null != dateRange && '' != dateRange) {
search.beginTime = this.dateRange[0];
search.endTime = this.dateRange[1];
}
return search;
}回顯數(shù)據(jù)字典
export function selectDictLabel(datas, value) {
var actions = [];
Object.keys(datas).map((key) => {
if (datas[key].dictValue == ('' + value)) {
actions.push(datas[key].dictLabel);
return false;
}
})
return actions.join('');
}通用下載方法
export function download(fileName) {
window.location.href = baseURL + "/common/download?fileName=" + encodeURI(fileName) + "&delete=" + true;
}字符串格式化(%s)
export function sprintf(str) {
var args = arguments, flag = true, i = 1;
str = str.replace(/%s/g, function () {
var arg = args[i++];
if (typeof arg === 'undefined') {
flag = false;
return '';
}
return arg;
});
return flag ? str : '';
}轉(zhuǎn)換字符串,undefined,null等轉(zhuǎn)化為""
export function praseStrEmpty(str) {
if (!str || str == "undefined" || str == "null") {
return "";
}
return str;
}構(gòu)造樹型結(jié)構(gòu)數(shù)據(jù)
/**
* 構(gòu)造樹型結(jié)構(gòu)數(shù)據(jù)
* @param {*} data 數(shù)據(jù)源
* @param {*} id id字段 默認 'id'
* @param {*} parentId 父節(jié)點字段 默認 'parentId'
* @param {*} children 孩子節(jié)點字段 默認 'children'
* @param {*} rootId 根Id 默認 0
*/
export function handleTree(data, id, parentId, children, rootId) {
if (!data || data.length === 0) {
return []
}
id = id || 'id'
parentId = parentId || 'parentId'
children = children || 'children'
rootId = rootId || 0
//對源數(shù)據(jù)深度克隆
const cloneData = JSON.parse(JSON.stringify(data))
//循環(huán)所有項
const treeData = cloneData.filter(father => {
let branchArr = cloneData.filter(child => {
//返回每一項的子級數(shù)組
return father[id] === child[parentId]
});
branchArr.length > 0 ? father.children = branchArr : '';
//返回第一層
return father[parentId] === rootId;
});
return treeData != '' ? treeData : data;
}到此這篇關(guān)于JS樣式獲取的封裝方法的文章就介紹到這了,更多相關(guān)JS樣式獲取封裝內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
JavaScript函數(shù)式編程實現(xiàn)介紹
函數(shù)式編程是一種編程范式,將整個程序都由函數(shù)調(diào)用以及函數(shù)組合構(gòu)成。 可以看成一條流水線,數(shù)據(jù)可以不斷地從一個函數(shù)的輸出流入另一個函數(shù)的輸入,最后輸出結(jié)果2022-09-09
可能被忽略的一些JavaScript數(shù)組方法細節(jié)
這篇文章主要給大家介紹了一些可能被忽略的JavaScript數(shù)組方法細節(jié),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-02-02
微信小程序授權(quán)登錄及解密unionId出錯的方法
這篇文章主要介紹了微信小程序授權(quán)登錄及解密unionId出錯的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-09-09

