简体   繁体   English

使用jQuery替换引力表单管理中字符串“表单”的所有实例

[英]Replacing all instances of the string “form” in gravity forms admin using jQuery

I've been asked that I change all instances of the word "form" to "block" as part of a WordPress app we are working on. 有人要求我将“ form”一词的所有实例更改为“ block”,作为我们正在开发的WordPress应用程序的一部分。 The replacement needs to happened throughout gravity forms admin section. 更换需要在整个重力表格管理部分进行。 Because gravity forms does not have an easy way to change this without editing the plugin I've opted to use jQuery to replace all instance on dom load. 因为重力形式没有一种简单的方法来更改它,而无需编辑插件,所以我选择使用jQuery替换dom加载时的所有实例。

The issue I'm facing however is that Gravity Forms dynamically places inline javascript into the body of the html. 但是,我面临的问题是Gravity Forms动态地将内联javascript放置到html的正文中。 Inline /w the displayed pages content. 内联/ w显示的页面内容。 So when I run say for instance: 因此,例如,当我运行时说:

jQuery("#wpbody-content").html(jQuery("#wpbody-content").html().replace(/Form/gi, 'Block'));

It ends up replacing important instance of the word "form" inside of those inline javascript inserts (which are used to create a new form using ajax for example). 最后,这些内联javascript插入内容(例如,用于使用ajax创建新表单)中替换单词“ form”的重要实例。

I was wondering if anyone had any ideas how I might do a replace of all instances of the word "form" that is actually html and not part of any inline javascript? 我想知道是否有人对如何替换实际上是html而不是任何内联javascript的“ form”一词的所有实例有任何想法?

I'm trying to get this: 我试图得到这个:

<div class="#wpbody-content">
    <div class="setting-row">
        <label for="new_form_description">Form Description</label><br>
        <textarea class="regular-text" id="new_form_description" tabindex="9001"></textarea>
    </div>

    <script type="text/javascript">

            jQuery(document).ready(function($) {

                 loadNewFormModal();

                function loadNewFormModal() {
                    resetNewFormModal();
                    tb_show('Create a New Form', '#TB_inline?width=375&amp;inlineId=gf_new_form_modal');
                    jQuery('#new_form_title').focus();
                    return false;
                }

            }

    </script>
</div>

to become this: 成为这个:

<div class="#wpbody-content">
    <div class="setting-row">
        <label for="new_form_description">Module Description</label><br>
        <textarea class="regular-text" id="new_form_description" tabindex="9001"></textarea>

    </div>

    <script type="text/javascript">

            jQuery(document).ready(function($) {

                 loadNewFormModal();

                function loadNewFormModal() {
                    resetNewFormModal();
                    tb_show('Create a New Form', '#TB_inline?width=375&amp;inlineId=gf_new_form_modal');
                    jQuery('#new_form_title').focus();
                    return false;
                }

            }

    </script>
</div>

and not this: 而不是这样:

<div class="#wpbody-content">
    <div class="setting-row">
        <label for="new_Block_description">Block Description</label><br>
        <textarea class="regular-text" id="new_Block_description" tabindex="9001"></textarea>
    </div>

    <script type="text/javascript">

            jQuery(document).ready(function($) {

                 loadNewBlockModal();

                function loadNewBlockModal() {
                    resetNewBlockModal();
                    tb_show('Create a New Block', '#TB_inline?width=375&amp;inlineId=gf_new_Block_modal');
                    jQuery('#new_Block_title').focus();
                    return false;
                }

            }

    </script>
</div>

You can do the following: 您可以执行以下操作:

  • Search all content for the desired word (Form) 在所有内容中搜索所需的单词(表格)
  • Check if the that word parent is not SCRIPT 检查该单词parent不是SCRIPT
  • Modify the content of that tag with the replaced word 用替换的单词修改该标签的内容

jsfiddle DEMO jsfiddle演示

$(function(){
    $('*:contains("Form")').each(function(){
        if($(this).children().length < 1){
            if($(this).prop("tagName") != "SCRIPT"){
                //$(this).html().replace(/Form/gi, 'Block');
                $(this).html($(this).html().replace(/Form/gi,"Block" ));
            }
        }
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM