如何在进入可视化编辑器时阻止Java代码被破坏

时间:2015-09-17 作者:Jordan

因此,我已经不得不让我的代码比需要的更难看,因为每当我尝试这样做时&&;wordpress决定我想要;amp,现在我发现,当我进入可视化视图时,该页面上定义的任何javascript都会中断。

例如,我现在拥有的代码(有效):

<script>
function checkCosts() {
var date = document.getElementsByName("date")[0].value;
var roomtype = document.getElementsByName("roomtype")[0].value;
var night = document.getElementsByName("night")[0].value;
var room = document.getElementsByName("room")[0].value;
var month = date.substring(0, 2);
var year = date.substring(8, 10);
var day = date.substring(4, 6);
var time = month.concat(year);
var fulldate = parseInt(day.concat(time));
var inttime = parseInt(time);
//=================================
//Date Range 1
//Check earliest date
if (inttime >= 415) {
    //Check latest date
    if (inttime <= 915) {
        //Classic Roomtype
        if (roomtype == "Classic") {
            //Nights Double
            if (room = "Double") {
                if (night == "3") {
                    document.getElementById("cost").innerHTML = "1,480";
                }
                if (night == "5") {
                    document.getElementById("cost").innerHTML = "1,450";
                }
                if (night == "7") {
                    document.getElementById("cost").innerHTML = "1,415";
                }
            }
            //Nights Single
            if (room = "Single") {
                if (night == "3") {
                    document.getElementById("cost").innerHTML = "2,575";
                }
                if (night == "5") {
                    document.getElementById("cost").innerHTML = "2,510";
                }
                if (night == "7") {
                    document.getElementById("cost").innerHTML = "2,440";
                }
            }
        }
        //End
        //Luxury Roomtype
        if (roomtype == "Luxury") {
            //Nights
            if (room = "Double") {
                if (night == "3") {
                    document.getElementById("cost").innerHTML = "1,620";
                }
                if (night == "5") {
                    document.getElementById("cost").innerHTML = "1,580";
                }
                if (night == "7") {
                    document.getElementById("cost").innerHTML = "1,535";
                }
            }
            //Nights Single
            if (room = "Single") {
                if (night == "3") {
                    document.getElementById("cost").innerHTML = "2,830";
                }
                if (night == "5") {
                    document.getElementById("cost").innerHTML = "2,760";
                }
                if (night == "7") {
                    document.getElementById("cost").innerHTML = "2,685";
                }
            }
        }
        //End
        //Suite Roomtype
        if (roomtype == "Suite") {
            //Nights
            if (room = "Double") {
                if (night == "3") {
                    document.getElementById("cost").innerHTML = "1,860";
                }
                if (night == "5") {
                    document.getElementById("cost").innerHTML = "1,815";
                }
                if (night == "7") {
                    document.getElementById("cost").innerHTML = "1,770";
                }
            }
            //Nights Single
            if (room = "Single") {
                if (night == "3") {
                    document.getElementById("cost").innerHTML = "3,300";
                }
                if (night == "5") {
                    document.getElementById("cost").innerHTML = "3,235";
                }
                if (night == "7") {
                    document.getElementById("cost").innerHTML = "3,145";
                }
            }
        }
        //End
    }
}
 //=================================
        //Date Range 2
        //Check earliest date
            //Check latest date

                //Classic Roomtype
                if (roomtype == "Classic") {
                    //Nights Double
                    if (room = "Double") {
                        if (night == "3") {
                            document.getElementById("cost").innerHTML = "1,795";
                        }
                        if (night == "5") {
                            document.getElementById("cost").innerHTML = "1,745";
                        }
                        if (night == "7") {
                            document.getElementById("cost").innerHTML = "1,700";
                        }
                    }
                    //Nights Single
                    if (room = "Single") {
                        if (night == "3") {
                            document.getElementById("cost").innerHTML = "3,185";
                        }
                        if (night == "5") {
                            document.getElementById("cost").innerHTML = "3,100";
                        }
                        if (night == "7") {
                            document.getElementById("cost").innerHTML = "3,015";
                        }
                    }
                }
                //End
                //Luxury Roomtype
                if (roomtype == "Luxury") {
                    //Nights
                    if (room = "Double") {
                        if (night == "3") {
                            document.getElementById("cost").innerHTML = "1,960";
                        }
                        if (night == "5") {
                            document.getElementById("cost").innerHTML = "1,905";
                        }
                        if (night == "7") {
                            document.getElementById("cost").innerHTML = "1,860";
                        }
                    }
                    //Nights Single
                    if (room = "Single") {
                        if (night == "3") {
                            document.getElementById("cost").innerHTML = "3,515";
                        }
                        if (night == "5") {
                            document.getElementById("cost").innerHTML = "3,425";
                        }
                        if (night == "7") {
                            document.getElementById("cost").innerHTML = "3,320";
                        }
                    }
                }
                //End
                //Suite Roomtype
                if (roomtype == "Suite") {
                    //Nights
                    if (room = "Double") {
                        if (night == "3") {
                            document.getElementById("cost").innerHTML = "1,860";
                        }
                        if (night == "5") {
                            document.getElementById("cost").innerHTML = "1,815";
                        }
                        if (night == "7") {
                            document.getElementById("cost").innerHTML = "1,770";
                        }
                    }
                    //Nights Single
                    if (room = "Single") {
                        if (night == "3") {
                            document.getElementById("cost").innerHTML = "4,505";
                        }
                        if (night == "5") {
                            document.getElementById("cost").innerHTML = "4,375";
                        }
                        if (night == "7") {
                            document.getElementById("cost").innerHTML = "4,245";
                        }
                    }
                }
                //End
            }
}
</script>
但只要我进入visual editor(我为其设计此网站的人无疑会这么做),代码就变成了

<script>// <![CDATA[
function checkCosts() {
var date = document.getElementsByName("date")[0].value;
var roomtype = document.getElementsByName("roomtype")[0].value;
var night = document.getElementsByName("night")[0].value;
var room = document.getElementsByName("room")[0].value;
var month = date.substring(0, 2);
var year = date.substring(8, 10);
var day = date.substring(4, 6);
var time = month.concat(year);
var fulldate = parseInt(day.concat(time));
var inttime = parseInt(time);
//=================================
//Date Range 1
//Check earliest date
if (inttime >= 415) {
    //Check latest date
    if (inttime <= 915) {
        //Classic Roomtype
        if (roomtype == "Classic") {
            //Nights Double
            if (room = "Double") {
                if (night == "3") {
                    document.getElementById("cost").innerHTML = "1,480";
                }
                if (night == "5") {
                    document.getElementById("cost").innerHTML = "1,450";
                }
                if (night == "7") {
                    document.getElementById("cost").innerHTML = "1,415";
                }
            }
            //Nights Single
            if (room = "Single") {
                if (night == "3") {
                    document.getElementById("cost").innerHTML = "2,575";
                }
                if (night == "5") {
                    document.getElementById("cost").innerHTML = "2,510";
                }
                if (night == "7") {
                    document.getElementById("cost").innerHTML = "2,440";
                }
            }
        }
        //End
        //Luxury Roomtype
        if (roomtype == "Luxury") {
            //Nights
            if (room = "Double") {
                if (night == "3") {
                    document.getElementById("cost").innerHTML = "1,620";
                }
                if (night == "5") {
                    document.getElementById("cost").innerHTML = "1,580";
                }
                if (night == "7") {
                    document.getElementById("cost").innerHTML = "1,535";
                }
            }
            //Nights Single
            if (room = "Single") {
                if (night == "3") {
                    document.getElementById("cost").innerHTML = "2,830";
                }
                if (night == "5") {
                    document.getElementById("cost").innerHTML = "2,760";
                }
                if (night == "7") {
                    document.getElementById("cost").innerHTML = "2,685";
                }
            }
        }
        //End
        //Suite Roomtype
        if (roomtype == "Suite") {
            //Nights
            if (room = "Double") {
                if (night == "3") {
                    document.getElementById("cost").innerHTML = "1,860";
                }
                if (night == "5") {
                    document.getElementById("cost").innerHTML = "1,815";
                }
                if (night == "7") {
                    document.getElementById("cost").innerHTML = "1,770";
                }
            }
            //Nights Single
            if (room = "Single") {
                if (night == "3") {
                    document.getElementById("cost").innerHTML = "3,300";
                }
                if (night == "5") {
                    document.getElementById("cost").innerHTML = "3,235";
                }
                if (night == "7") {
                    document.getElementById("cost").innerHTML = "3,145";
                }
            }
        }
        //End
    }
}
 //=================================
        //Date Range 2
        //Check earliest date
            //Check latest date

                //Classic Roomtype
                if (roomtype == "Classic") {
                    //Nights Double
                    if (room = "Double") {
                        if (night == "3") {
                            document.getElementById("cost").innerHTML = "1,795";
                        }
                        if (night == "5") {
                            document.getElementById("cost").innerHTML = "1,745";
                        }
                        if (night == "7") {
                            document.getElementById("cost").innerHTML = "1,700";
                        }
                    }
                    //Nights Single
                    if (room = "Single") {
                        if (night == "3") {
                            document.getElementById("cost").innerHTML = "3,185";
                        }
                        if (night == "5") {
                            document.getElementById("cost").innerHTML = "3,100";
                        }
                        if (night == "7") {
                            document.getElementById("cost").innerHTML = "3,015";
                        }
                    }
                }
                //End
                //Luxury Roomtype
                if (roomtype == "Luxury") {
                    //Nights
                    if (room = "Double") {
                        if (night == "3") {
                            document.getElementById("cost").innerHTML = "1,960";
                        }
                        if (night == "5") {
                            document.getElementById("cost").innerHTML = "1,905";
                        }
                        if (night == "7") {
                            document.getElementById("cost").innerHTML = "1,860";
                        }
                    }
                    //Nights Single
                    if (room = "Single") {
                        if (night == "3") {
                            document.getElementById("cost").innerHTML = "3,515";
                        }
                        if (night == "5") {
                            document.getElementById("cost").innerHTML = "3,425";
                        }
                        if (night == "7") {
                            document.getElementById("cost").innerHTML = "3,320";
                        }
                    }
                }
                //End
                //Suite Roomtype
                if (roomtype == "Suite") {
                    //Nights
                    if (room = "Double") {
                        if (night == "3") {
                            document.getElementById("cost").innerHTML = "1,860";
                        }
                        if (night == "5") {
                            document.getElementById("cost").innerHTML = "1,815";
                        }
                        if (night == "7") {
                            document.getElementById("cost").innerHTML = "1,770";
                        }
                    }
                    //Nights Single
                    if (room = "Single") {
                        if (night == "3") {
                            document.getElementById("cost").innerHTML = "4,505";
                        }
                        if (night == "5") {
                            document.getElementById("cost").innerHTML = "4,375";
                        }
                        if (night == "7") {
                            document.getElementById("cost").innerHTML = "4,245";
                        }
                    }
                }
                //End
            }
}
// ]]></script>
这是行不通的。无论页面上的javascript多么简单或复杂,都会发生这种情况。

有没有什么方法可以阻止这种情况的发生,而不必教我正在制作的人如何处理HTML,这样他们就可以编辑一些东西

1 个回复
SO网友:TheDeadMedic

有什么办法阻止这一切吗

编辑器中没有JavaScript。它很脏,很难维护(正如你发现的那样)。

相反,您可以:

使用page template

  • Use custom fields (post meta)
  • functions.php

    function wpse_202946_script() {
        if ( is_page( \'id_or_title_or_slug\' ) ) {
            wp_enqueue_script(
                \'my-theme-script-name\',
                get_template_directory_uri() . \'/path/to/script.js\',
                null, // Dependencies
                null, // Version
                true  // In footer
            );
        }
    }
    
    add_action( \'wp_enqueue_scripts\', \'wpse_202946_script\' );