如何移除WooCommerce账户仪表板中的「(not User1? Log out)」
移除WooCommerce账户仪表板的「(not User1? Log out)」内容
你之前的JS脚本没生效,核心是逻辑偏差——它把用户名所在的<strong>元素也删掉了,只留下了"Hello",没正确定位要移除的目标内容。下面给你两种无需修改插件代码的可行方案:
方案一:修正JavaScript脚本
这个脚本会精准保留「Hello + 用户名」部分,移除后面的注销提示内容:
document.addEventListener('DOMContentLoaded', function() { const greetingPara = document.querySelector('.woocommerce-MyAccount-content p:first-child'); if (greetingPara) { // 截取到"(not"之前的内容,保留Hello和用户名 const trimmedContent = greetingPara.innerHTML.split('(not')[0].trim(); greetingPara.innerHTML = trimmedContent; } });
如果担心innerHTML操作可能引发的问题,可以用更安全的节点操作方式:
document.addEventListener('DOMContentLoaded', function() { const greetingPara = document.querySelector('.woocommerce-MyAccount-content p:first-child'); if (greetingPara) { let childNodes = Array.from(greetingPara.childNodes); // 找到第一个strong元素的位置 const strongPos = childNodes.findIndex(node => node.tagName === 'STRONG'); if (strongPos !== -1) { // 删除strong之后的所有节点 for (let i = strongPos + 1; i < childNodes.length; i++) { greetingPara.removeChild(childNodes[i]); } // 清理末尾多余空格 greetingPara.textContent = greetingPara.textContent.trim(); } } });
方案二:用CSS快速隐藏
如果不需要保留交互性,CSS隐藏是更简单的方法,只需添加以下样式:
.woocommerce-MyAccount-content p:first-child { overflow: hidden; position: relative; } .woocommerce-MyAccount-content p:first-child::after { content: ''; position: absolute; top: 0; /* 这里的left值需要根据实际文本长度调整,确保覆盖后面的注销提示 */ left: calc(100% - 160px); width: 200px; height: 100%; background: #ffffff; /* 替换成你页面的背景色 */ }
内容的提问来源于stack exchange,提问作者Muhammad Ahmad
相关产品推荐
相关产品推荐

