duangan7834 2019-03-10 01:33
浏览 85
已采纳

自定义的WooCommerce消息删除HTML标记上的onClick事件

I've overridden notices/success.php template file, modifying the content this way:

<?php foreach ( $messages as $message ) : ?>
    <div class="woocommerce-message" role="alert">
        <span>
            <?php
            echo wc_kses_notice( $message );
            ?>
        </span>
        <div onClick="alert('LOL')">TEST</div>
    </div>
<?php endforeach; ?>

When I now load the page, the elements are all generated but my onClick is removed:

<div class="woocommerce-message" role="alert"> <span> Kontodetails erfolgreich geändert. </span><div>TEST</div></div>

Why is this happening and how can I keep my onClick in every div or span within this message?

  • 写回答

1条回答 默认 最新

  • duanchui1251 2019-03-10 03:36
    关注

    To avoid this problem, instead of using an onClick event on the <div> tag, you should use a jQuery script:

    1) The overridden template notices/success.php file (excerpt):

    <?php foreach ( $messages as $message ) : ?>
        <div class="woocommerce-message" role="alert">
            <span>
            <?php
                echo wc_kses_notice( $message );
            ?>
            </span>
        </div>
        <div class="message-click">TEST</div>
    <?php endforeach; ?>
    

    2) The jQuery script:

    add_action( 'wp_footer', 'message_onclick_event');
    function message_onclick_event() {
        ?>
        <script>
        jQuery(function($){
            $(document.body).on('click', 'div.message-click', function(){
                alert('LOL');
            })
        });
        </script>
        <?php
    }
    

    Code goes in function.php file of your active child theme (or active theme), or in an external registered JS file.

    3) The html output:

    <div class="woocommerce-message" role="alert">
        <span>Hello World</span>
    </div>
    <div class="message-click">TEST</div>
    

    4) A screenshot (when clicking on "TEST"):

    enter image description here

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程