getEle()

本文详细介绍了DOM操作的各种方法,包括元素的位置设置、样式属性调整、事件监听等,为开发者提供了全面的DOM操作技巧。

位置设置:

getX()   取得相对于页面的x坐标
getY()   取得相对于页面的y坐标
getXY() 取得相对于页面的x,y坐标,用法:ele.getXY()[0]/[1]
getOffsetsTo(el)  取得相对元素el的坐标,返回值[x,y]
setX(x,animate)   设相对于页面的x坐标,animate为true则开启默认动画效果
setY(y,animate)  同上
setLeft(left)   设style.left
setTop(top)    设style.top
setRight(right)  设style.right
setBottom(bottom) 设style.bottom
setXY(pos,animate) 设相对于页面的x,y,相当于setX,setY。用法:setXY([x,y],true)
setLocation(x,y,animate) 相当于setXY。
getRegion()        return {top=t,left=l,width=w,height=r}
getHeight(true|false)    true不包括边框、内边距,false包括
getBorderWidth(anthor)
getPadding(anthor)
getComputedHeight()  包括边框、内边距
getComputedWidth()
getSize()   相当于getWidth、getHeight   {width:w,height:h}
getStyleSize()   取style.width、style.height,且不包括边框{width:w,height:h}
getViewSize()    取视口大小{width:w,height:h}
getValue()     如果有value属必就取它
setWidth(width,animate)     设style.width
setHeight(height,animate)    设style.height
setSize(width,height,animate)
setBounds(x,y,width,height,animate)   相当于setSize、setXY
setRegion(region,animate)      相当于setBounds
getScroll()            取得当前视口在文档中水平、垂直方向上的偏移,返回{left:l,top:t}

setOpacity(opacity,animate)
getLeft(local)    false相当于getX,true相当于style.left
getRight(local)   false相当于getX+getWidth,true相当于getLeft(true)+getWidth()
getTop(local)   false相当于getY,true相当于style.top
getBottom(local)
position(pos,zIndex,x,y)  string:pos,取static,relative,absolute,fixed
                          zIndex设z序,x,y用来调用setXY的
clearPositionsing(value)  作用非得看代码
    clearPositioning : function(value){
        value = value ||'';
        this.setStyle({
            "left": value,
            "right": value,
            "top": value,
            "bottom": value,
            "z-index": "",
            "position" : "static"
        });
        return this;
    }

getPositioning()  取值格式如上{……}
setPositioning(pc)  用法同applyStyles
setLeftTop(left,top)  就是设style.left,style.top


moveTo(x,y,animate)    相当于setXY
move(direction,distance,animate)   很有用,direction移动方向,取t,l,r,b。
                    distance是移动的距离,animate是否启用动画效果

getAnchorXY(anchor,local,s)   取得对齐到某处的页面x,y坐标
getCenterXY()          取得对齐到视口中央的页面x,y坐标
getAlignToXY(el,p,o)       取得对齐到某元素某处的页面x,y坐标

alignTo(element,position,offsets,animate)  对齐到元素的某处,允许偏移、动画
anchorTo(el,alignment,offsets,animate,monitorScroll,callback)  对齐到元素


center(centerIn)   对齐到视口中央
getBox(contentBox,local)  contentBox=false包括边框、内边距 local=false获取页面坐标
setBox(box,adjust,animate)   box包括边框、内边距
getFrameWidth(sides,onlycontentBox)

repaint()    强制刷新元素


关于样式、属性设置

addClass(String/Array className )
removeClass(String/Array className)
replaceClass(String oldClassName, String newClassName )
radioClass(String/Array className )
toggleClass(String className )
setStyle(name,value)
getStyle(name)
hasClass(className)

addClassOnClick(classname)
addClassOnFocus(classname)
addClassOnOver(classname)
hover(classsname,bool preventFlicker)

页点操作:
append(ele)
appendTo(ele)
replace(ele)
replaceWith(ele)
insertBefore(ele)
insertAfter(ele)
insertFirst(ele)
insertHtml(where,html,returnEl)
insertSibling(el,where,returnDom)
remove()
createChild(config,HTMLElement insertbefore,returndom)


first(selector,returndom)
last(selector,returndom)
next(selector,returndom)
prev(selector,returndom)
parent(selector,returndom)
child(selector,returndom)
up(selector,maxdepth)
down(selector,maxdepth)

query(selector)

contains(HTMLElement/string el)

show(animate)
hide(animate)
toggle(animate)
setVisible(boolean visible,animate)


update(html, loadScripts, callback)

特殊的
hover( Function overFn, Function outFn, [Object scope] ) : Ext.Element
mask(msg, msgCls)
unmask()
load( String/Function url, [String/Object params], [Function callback], [Boolean discardUrl] )
focus()
blur()


事件注册:
on(eventname,fun)
un(eventname,fun)
addListenser/removeListenser/removeAllListeners

<template> <myModal :params="myModalParams" :modalWidthSize="'small'" :visible="visible" @submit="handleOk" @cancel="handleCancel" :getContainer="getEle" destroyOnClose > <template v-slot:header><span></span></template> <template v-slot:body> <div class="weight-config-box"> 风险评价等级 <div class="grade-box"> <!-- 优 --> <div class="excellent"> <div class="text">{{ $t('优:') }}</div> <div class="num-box"> <span>分数≤</span> <a-input v-model="riskScores.excellent" @input="e => handleInput(e, 'excellent')" style="width: 100px" placeholder="0-1" /> </div> <div>(0-1)</div> </div> <!-- 良 --> <div class="good"> <div class="text">{{ $t('良:') }}</div> <div class="num-box"> <a-input v-model="riskScores.goodMin" @input="e => handleInput(e, 'goodMin')" style="width: 100px" placeholder="0-1" /> <span><分数≤</span> <a-input v-model="riskScores.goodMax" @input="e => handleInput(e, 'goodMax')" style="width: 100px" placeholder="0-1" /> </div> <div>(0-1)</div> </div> <!-- 差 --> <div class="bad"> <div class="text">{{ $t('差:') }}</div> <div class="num-box"> <a-input v-model="riskScores.bad" @input="e => handleInput(e, 'bad')" style="width: 100px" placeholder="0-1" /> <span><分数</span> </div> <div>(0-1)</div> </div> </div> 报警评价等级 <div class="grade-box"> <!-- 优 --> <div class="excellent"> <div class="text">{{ $t('优:') }}</div> <div class="num-box"> <span>分数≤</span> <a-input v-model="alarmScores.excellent" @input="e => handleInput(e, 'excellent')" style="width: 100px" placeholder="0-1" /> </div> <div>(0-1)</div> </div> <!-- 良 --> <div class="good"> <div class="text">{{ $t('良:') }}</div> <div class="num-box"> <a-input v-model="alarmScores.goodMin" @input="e => handleInput(e, 'goodMin')" style="width: 100px" placeholder="0-1" /> <span><分数≤</span> <a-input v-model="alarmScores.goodMax" @input="e => handleInput(e, 'goodMax')" style="width: 100px" placeholder="0-1" /> </div> <div>(0-1)</div> </div> <!-- 差 --> <div class="bad"> <div class="text">{{ $t('差:') }}</div> <div class="num-box"> <a-input v-model="alarmScores.bad" @input="e => handleInput(e, 'bad')" style="width: 100px" placeholder="0-1" /> <span><分数</span> </div> <div>(0-1)</div> </div> </div> </div> </template> </myModal> </template> <script> import myModal from '@/components/scfComponents/modalComponents/modal.vue'; export default { components: { myModal }, data() { return { visible: false, errorMsg: '', riskScores: { excellent: '', // 优的上限 goodMin: '', // 良的下限 goodMax: '', // 良的上限 bad: '', // 差的下限 }, alarmScores: { excellent: '', // 优的上限 goodMin: '', // 良的下限 goodMax: '', // 良的上限 bad: '', // 差的下限 }, myModalParams: { title: '编辑权重', }, }; }, methods: { // DOM挂载点 getEle() { return document.querySelector('.standard-page'); }, // 通用输入处理 handleInput(e, field) { let val = e.target.value; // 1. 过滤非法字符 val = val.replace(/[^\d.]/g, ''); // 2. 处理小数点问题 val = val.replace(/\.{2,}/g, '.'); if (val.startsWith('.')) val = '0.'; // 3. 限制小数位数 const decimalIndex = val.indexOf('.'); if (decimalIndex !== -1 && val.length - decimalIndex > 5) { val = val.substring(0, decimalIndex + 5); } // 4. 数值范围限制 if (val !== '' && val !== '.') { const numVal = parseFloat(val); if (!isNaN(numVal)) { if (numVal < 0) val = '0'; else if (numVal > 1) val = '1'; } } // 更新对应字段 this.scores[field] = val; this.errorMsg = ''; // 清除错误提示 }, // 打开弹窗(可初始化数据) showModal(initialScores) { if (initialScores) { this.scores = { ...initialScores }; } this.visible = true; this.errorMsg = ''; }, // 提交验证 handleOk() { // 验证必填 const fields = ['excellent', 'goodMin', 'goodMax', 'bad']; for (const field of fields) { if (this.scores[field] === '') { this.errorMsg = this.$t('所有字段必须填写'); return; } } // 数值验证 const excellent = parseFloat(this.scores.excellent); const goodMin = parseFloat(this.scores.goodMin); const goodMax = parseFloat(this.scores.goodMax); const bad = parseFloat(this.scores.bad); // 区间逻辑验证 if (goodMin >= goodMax) { this.errorMsg = this.$t('良的下限必须小于上限'); return; } if (excellent >= goodMin) { this.errorMsg = this.$t('优的上限必须小于良的下限'); return; } if (goodMax >= bad) { this.errorMsg = this.$t('良的上限必须小于差的下限'); return; } // 数值范围验证 if ([excellent, goodMin, goodMax, bad].some(v => v < 0 || v > 1)) { this.errorMsg = this.$t('所有值必须在0-1范围内'); return; } // 验证通过,提交数据 this.$emit('submit', { ...this.scores }); this.visible = false; }, // 取消 handleCancel() { this.visible = false; }, }, }; </script> <style lang="less" scoped> .weight-config-box { width: 100%; height: 100%; text-align: left; box-sizing: border-box; h2 { color: #fff; } .grade-box { > div { display: flex; align-items: center; margin-top: 12px; .num-box { width: 260px; margin: 0 12px; display: flex; align-items: center; justify-content: space-between; } } .excellent .num-box { justify-content: flex-end; } .bad .num-box { justify-content: flex-start; } .error-msg { margin-top: 16px; } } } </style>
07-02
<template> <myModal :params="myModalParams" :modalWidthSize="'small'" :visible="visible" @submit="handleOk" @cancel="handleCancel" :getContainer="getEle" destroyOnClose > <template v-slot:header><span></span></template> <template v-slot:body> <div class="weight-config-box"> <div class="grade-box"> <div class="excellent"> <div class="text">{{ $t('优:') }}</div> <div class="num-box"> <span>分数≤</span> <a-input v-model="value" @input="handleInput" style="width: 100px" placeholder="请输入0-1之间的数字" /> </div> <div>(0-1)</div> </div> <div class="good"> <div class="text">{{ $t('良:') }}</div> <div class="num-box"> <a-input v-model="value" @input="handleInput" style="width: 100px" placeholder="请输入0-1之间的数字" /> <span><分数≤</span> <a-input v-model="value" @input="handleInput" style="width: 100px" placeholder="请输入0-1之间的数字" /> </div> <div>(0-1)</div> </div> <div class="bad"> <div class="text">{{ $t('差:') }}</div> <div class="num-box"> <a-input v-model="value" @input="handleInput" style="width: 100px" placeholder="请输入0-1之间的数字" /> <span><分数</span> </div> <div>(0-1)</div> </div> </div> </div> </template> </myModal> </template> <script> import myModal from '@/components/scfComponents/modalComponents/modal.vue'; export default { components: { myModal }, data() { return { value: '', visible: false, myModalParams: { title: '编辑权重', }, }; }, methods: { // 挂载dom getEle() { return document.querySelector('.standard-page'); }, handleInput(e) { let val = e.target.value; // 1. 过滤非数字和小数点 val = val.replace(/[^\d.]/g, ''); // 2. 过滤多余的小数点 val = val.replace(/\.{2,}/g, '.'); val = val.replace(/^\./g, '0.'); // 处理开头的小数点 // 3. 限制小数位数(可选) if (val.includes('.')) { const parts = val.split('.'); if (parts[1].length > 4) { // 限制4位小数 val = parts[0] + '.' + parts[1].slice(0, 4); } } // 4. 强制范围在0-1之间 if (val !== '') { const num = parseFloat(val); if (isNaN(num) || num < 0) val = '0'; else if (num > 1) val = '1'; } this.value = val; }, // 打开弹窗 showModal() { this.visible = true; }, // 确定 handleOk() { this.visible = false; }, // 取消 handleCancel() { this.visible = false; }, }, }; </script> <style lang="less" scoped> .weight-config-box { width: 100%; height: 100%; text-align: left; box-sizing: border-box; h2 { color: #fff; } .grade-box { > div { display: flex; align-items: center; margin-top: 12px; .num-box { width: 260px; margin:0 12px; display: flex; align-items: center; justify-content: space-between; } } .excellent .num-box { justify-content: flex-end; } .bad .num-box { justify-content: flex-start; } } } </style>
07-02
<template> <myModal :params="myModalParams" :modalWidthSize="'small'" :visible="visible" @submit="handleOk" @cancel="handleCancel" :getContainer="getEle" destroyOnClose > <template v-slot:header><span></span></template> <template v-slot:body> <div class="weight-config-box"> <!-- 风险评价等级 --> <div class="config-section"> <h3>风险评价等级</h3> <div class="grade-box"> <!-- 优 --> <div class="excellent"> <div class="text">{{ $ t('优:') }}</div> <div class="num-box"> <span>分数≤</span> <a-input v-model="riskScores.excellent" @input="e => handleInput(e, 'risk', 'excellent')" style="width: 100px" placeholder="0-1" /> </div> <div>(0-1)</div> </div> <!-- 良 --> <div class="good"> <div class="text">{{ $ t('良:') }}</div> <div class="num-box"> <a-input v-model="riskScores.goodMin" @input="e => handleInput(e, 'risk', 'goodMin')" style="width: 100px" placeholder="0-1" /> <span><分数≤</span> <a-input v-model="riskScores.goodMax" @input="e => handleInput(e, 'risk', 'goodMax')" style="width: 100px" placeholder="0-1" /> </div> <div>(0-1)</div> </div> <!-- 差 --> <div class="bad"> <div class="text">{{ $ t('差:') }}</div> <div class="num-box"> <a-input v-model="riskScores.bad" @input="e => handleInput(e, 'risk', 'bad')" style="width: 100px" placeholder="0-1" /> <span><分数</span> </div> <div>(0-1)</div> </div> </div> <!-- 风险评价错误提示 --> <div v-if="riskErrorMsg" class="error-msg"> <a-alert type="error" :message="riskErrorMsg" banner /> </div> </div> <!-- 报警评价等级 --> <div class="config-section"> <h3>报警评价等级</h3> <div class="grade-box"> <!-- 优 --> <div class="excellent"> <div class="text">{{ $ t('优:') }}</div> <div class="num-box"> <span>分数≤</span> <a-input v-model="alarmScores.excellent" @input="e => handleInput(e, 'alarm', 'excellent')" style="width: 100px" placeholder="0-1" /> </div> <div>(0-1)</div> </div> <!-- 良 --> <div class="good"> <div class="text">{{ $ t('良:') }}</div> <div class="num-box"> <a-input v-model="alarmScores.goodMin" @input="e => handleInput(e, 'alarm', 'goodMin')" style="width: 100px" placeholder="0-1" /> <span><分数≤</span> <a-input v-model="alarmScores.goodMax" @input="e => handleInput(e, 'alarm', 'goodMax')" style="width: 100px" placeholder="0-1" /> </div> <div>(0-1)</div> </div> <!-- 差 --> <div class="bad"> <div class="text">{{ $ t('差:') }}</div> <div class="num-box"> <a-input v-model="alarmScores.bad" @input="e => handleInput(e, 'alarm', 'bad')" style="width: 100px" placeholder="0-1" /> <span><分数</span> </div> <div>(0-1)</div> </div> </div> <!-- 报警评价错误提示 --> <div v-if="alarmErrorMsg" class="error-msg"> <a-alert type="error" :message="alarmErrorMsg" banner /> </div> </div> </div> </template> </myModal> </template> <script> import myModal from '@/components/scfComponents/modalComponents/modal.vue'; export default { components: { myModal }, data() { return { visible: false, riskErrorMsg: '', // 独立错误提示 alarmErrorMsg: '', // 独立错误提示 riskScores: { excellent: '', goodMin: '', goodMax: '', bad: '' }, alarmScores: { excellent: '', goodMin: '', goodMax: '', bad: '' }, myModalParams: { title: '编辑权重', }, }; }, methods: { getEle() { return document.querySelector('.standard-page'); }, // 修正输入处理方法 handleInput(e, type, field) { let val = e.target.value; // 过滤非法字符 val = val.replace(/[^\d.]/g, ''); // 处理小数点问题 val = val.replace(/^\./, '0.').replace(/\.{2,}/g, '.'); // 更新对应数据对象 if (type === 'risk') { this.riskScores[field] = val; this.riskErrorMsg = ''; // 清除当前区域错误 } else { this.alarmScores[field] = val; this.alarmErrorMsg = ''; } }, // 修正初始化方法 showModal(riskData, alarmData) { if (riskData) this.riskScores = { ...riskData }; if (alarmData) this.alarmScores = { ...alarmData }; this.visible = true; this.riskErrorMsg = ''; this.alarmErrorMsg = ''; }, // 重构验证逻辑 handleOk() { const riskValid = this.validateScores(this.riskScores, 'risk'); const alarmValid = this.validateScores(this.alarmScores, 'alarm'); if (riskValid && alarmValid) { this.$ emit('submit', { riskScores: { ...this.riskScores }, alarmScores: { ...this.alarmScores } }); this.visible = false; } }, // 通用验证方法 validateScores(scores, type) { const fields = ['excellent', 'goodMin', 'goodMax', 'bad']; const errorTarget = type === 'risk' ? 'riskErrorMsg' : 'alarmErrorMsg'; // 空值检查 for (const field of fields) { if (scores[field] === '') { this[errorTarget] = this.$ t('所有字段必须填写'); return false; } } // 数字转换 const excellent = parseFloat(scores.excellent); const goodMin = parseFloat(scores.goodMin); const goodMax = parseFloat(scores.goodMax); const bad = parseFloat(scores.bad); // 数字有效性检查 if ([excellent, goodMin, goodMax, bad].some(isNaN)) { this[errorTarget] = this.$ t('请输入有效的数字'); return false; } // 范围检查 (0-1) if ([excellent, goodMin, goodMax, bad].some(v => v < 0 || v > 1)) { this[errorTarget] = this.$ t('所有值必须在0-1范围内'); return false; } // 逻辑关系验证 if (goodMin >= goodMax) { this[errorTarget] = this.$ t('良的下限必须小于上限'); return false; } if (excellent >= goodMin) { this[errorTarget] = this.$ t('优的上限必须小于良的下限'); return false; } if (goodMax >= bad) { this[errorTarget] = this.$ t('良的上限必须小于差的下限'); return false; } return true; }, handleCancel() { this.visible = false; }, }, }; </script> <style lang="less" scoped> .weight-config-box { width: 100%; height: 100%; text-align: left; box-sizing: border-box; .config-section { margin-bottom: 24px; h3 { margin-bottom: 12px; font-weight: 500; } } .grade-box { > div { display: flex; align-items: center; margin-top: 12px; .num-box { width: 260px; margin: 0 12px; display: flex; align-items: center; justify-content: space-between; } } .excellent .num-box { justify-content: flex-end; } .bad .num-box { justify-content: flex-start; } .error-msg { margin-top: 16px; } } } </style> 只能输入0-1之内数字
最新发布
07-02
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值