我想要替换WooCommerce中悬停在the product image上的产品标题,而不是标题本身.我很感激任何人的建议,因为我找不到任何与我的问题有关的答案.

我试过使用CSS:hover::after::after:hover,但我无法更改产品标题文本,因为悬停操作实际上与图像相关,而不是标题.所以我被困住了,不确定如何解决这个问题.

推荐答案

更新(code improvement: enable the feature in product shortcode and blocks)

对于WooCommerce单一产品页面,您可以使用以下选项来更改产品标题,当您将产品图像"悬停"时:

add_action('woocommerce_before_single_product_summary', 'single_product_script_js' );
function single_product_script_js() {
    // Here set the text replacement for the product title
    $text = __("Text replacement", "woocommerce");
    
    wc_enqueue_js("const productTitle  = $('.product_title').html(), textReplacement = '{$text}';
    $('body').on('mouseover mouseout', '.woocommerce-product-gallery__image', function(e) {
        $('.product_title').html(e.type === 'mouseover' ? textReplacement : productTitle);
    });");
}

代码位于您的子主题的unctions.php文件中(或在插件中).经过测试,效果良好.


初始答案代码:

add_action('wp_footer', 'wp_footer_product_script_js' );
function wp_footer_product_script_js() {
    if( is_product() ) :
    // Here set the text replacement for the product title
    $text = __("Text replacement", "woocommerce");
    ?>
    <script>
    jQuery(function($) {
        const productTitle  = $('.product_title').html(),    
            textReplacement = '<?php echo $text; ?>';
        $('body').on('mouseover mouseout', '.woocommerce-product-gallery__image', function(e) {
            $('.product_title').html(e.type === 'mouseover' ? textReplacement : productTitle);
        });
    });
    </script>
    <?php
    endif;
}

Javascript相关问答推荐

如何使用JavaScript对切换的声音设置音量

如果没有尾随斜线,托管在收件箱中的React/Vite将无法工作

积分计算和 colored颜色 判断错误

为什么从liveWire info js代码传递数组我出现错误?

如何分配类型脚本中具有不同/额外参数的函数类型

在页面上滚动 timeshift 动垂直滚动条

如何将Map字符串,布尔值转换为{key:string;value:bo布尔值;}[]?<>

我的角模板订阅后不刷新'

这个值总是返回未定义的-Reaction

使用Google API无法进行Web抓取

闭包是将值复制到内存的另一个位置吗?

React.Development.js和未捕获的ReferenceError:未定义useState

确保函数签名中的类型安全:匹配值

环境值在.js文件/Next.js中不起作用

如何在不影响隐式类型的情况下将类型分配给对象?

查询参数中的JAVASCRIPT/REACT中的括号

当用户点击保存按钮时,如何实现任务的更改?

无法重定向到Next.js中的动态URL

在D3条形图中对具有相同X值的多条记录进行分组

不协调嵌入图片