求助:PageSpeed Insights检测出woocommerce-Price-currencySymbol标签导致DOM元素过多的解决方法
Issue: Excessive DOM Elements from
<span class="woocommerce-Price-currencySymbol"> (PageSpeed Insights) 最近用PageSpeed Insights检测网站性能时,发现大量的<span class="woocommerce-Price-currencySymbol">标签触发了DOM元素过多(Excessive DOM elements)的性能问题。我留意到不少开发者都遇到了这个相同的问题,但目前还没有公认的有效解决方案。
我自己尝试了以下两种代码方案,遗憾的是都没能解决问题:
尝试的代码方案1
通过woocommerce_currency_symbol过滤器直接替换货币符号文本,尝试避免生成单独的span标签:
add_filter('woocommerce_currency_symbol', 'change_existing_currency_symbol', 10, 2); function change_existing_currency_symbol( $currency_symbol, $currency ) { switch( $currency ) { case 'AUD': $currency_symbol = 'AUD'; break; } return $currency_symbol; } include_once __DIR__ . '/theme-includes/theme-functions.php';
尝试的代码方案2
通过formatted_woocommerce_price过滤器修改价格的HTML结构,尝试将货币符号合并到金额的span标签中:
add_filter( 'formatted_woocommerce_price', 'span_custom_prc', 10, 5 ); function span_custom_prc( $number_format, $price, $decimals, $decimal_separator, $thousand_separator) { return "<span class='woocommerce-Price-amount amount'>" . $number_format . " </span>"; }
内容的提问来源于stack exchange,提问作者jollysea
相关产品推荐
相关产品推荐

