headerRight.style.marginBottom 怎么去掉单位px

文章介绍了如何使用JavaScript的parseInt()函数来处理带有px单位的CSS样式属性,例如marginBottom,将字符串值转换为整数后,再拼接回px单位的字符串形式。
    <div class="t_header t_header_right" style="margin-bottom: 5px;">
        <span>用户</span>
    </div>

去掉单位"px",可以使用JavaScript的字符串处理方法来实现。可以使用parseInt()函数将字符串转换为整数,然后再将其转换回字符串。以下是一个示例代码:

let headerRight = document.getElementsByClassName("t_header_right")[0];
let headerRightHeight = headerRight.offsetHeight;
let marginBottom = headerRight.style.marginBottom;
let marginBottomWithoutPx = parseInt(marginBottom) + "px";

在上述代码中,marginBottom是一个带有"px"单位的字符串,parseInt()函数将其转换为整数,然后再通过字符串拼接将其转换回带有"px"单位的字符串。

import { getAppliedHistoryArr, setAppliedHistoryArr, setUploadedImages, setCurrentAppliedColor, setCurrentAppliedImage, setColorImage } from './state.js'; import { switchToMode } from './mode-manager.js'; import { forcePickrSyncColor } from './color-mode.js'; // 添加历史记录项并渲染 export function addToAppliedHistory(item) { let arr = getAppliedHistoryArr(); arr = arr.filter(h => { if (item.id && h.id) return h.id !== item.id; if (item.color && h.color) return h.color !== item.color; return true; }); arr.unshift(item); if (arr.length > 10) arr = arr.slice(0, 10); setAppliedHistoryArr(arr); renderAppliedHistory(); } // 渲染历史记录到页面 export function renderAppliedHistory() { let arr = getAppliedHistoryArr(); const container = document.querySelector('.applied-history-container'); if (!container) return; container.innerHTML = ''; arr.slice(0, 10).forEach((item, idx) => { const div = document.createElement('div'); div.className = 'applied-history-block' + (idx === 0 ? ' active' : ''); div.style.cursor = 'pointer'; div.style.border = idx === 0 ? '3px solid #1746a2' : '1.5px solid #ccc'; div.style.marginBottom = '4px'; div.style.height = '48px'; div.style.borderRadius = '6px'; div.style.overflow = 'hidden'; if (item.url) { const img = document.createElement('img'); img.src = item.url; img.style.width = '100%'; img.style.height = '100%'; img.style.objectFit = 'cover'; div.appendChild(img); } else if (item.data && item.data.startsWith('data:image')) { const img = document.createElement('img'); img.src = item.data; img.style.width = '100%'; img.style.height = '100%'; img.style.objectFit = 'cover'; div.appendChild(img); } else if (item.color) { div.style.background = item.color; } div.onclick = function() { // 应用历史项 - 优先根据type属性判断 if (item.type === 'color' || item.color) { setColorImage(item); 这些代码应该放在vue里面的哪个结构中?
最新发布
07-24
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值