簡體   English   中英

javascript 函數在 wordpress 中的位置

[英]Where to place javascript functions in wordpress

我對 wordpress 有點陌生,並創建了一個自定義頁面/模板來實現一些基本的計算器操作。 為此,我有一個 html 表單,它在輸入上執行 javascript function:

形式:

<form oninput="T_Convert()" class="calculator">
        <h6 class="three-col">Temperature to Convert</h6>
        <h6 class="three-col">From</h6>
        <h6 class="three-col">Converted Temperature</h6>

                <input type="number" step="any" id="Temp" name="Temp" class="three-col">
                <select id="conversion_T" name="conversion" class="three-col">
                    <option value="C-F" selected>Celsius to Fahrenheit</option>
                    <option value="F-C">Fahrenheit to Celsius</option>
                </select>
            <p id="temperature" class="three-col"></p>
</form>

js function in functions.php:

function add_js_functions(){
?>
<script>
    function T_Convert() {
        var num = document.getElementById("Temp").value;
        var convert = document.getElementById("conversion_T").value;
        if(convert == "C-F"){
            num = num*9/5 + 32;
            document.getElementById("temperature").innerHTML = num.toFixed(2) + " Degrees Fahrenheit";
        }
        else{
            num = (num-32)*5/9
            document.getElementById("temperature").innerHTML = num.toFixed(2) + " Degrees Fahrenheit";
        }
    }
</script>
}
add_action('wp_head','add_js_functions');

我想知道將 javascript 函數保留在 functions.php 文件中是否是一種好習慣,或者我是否應該將其放在單獨的文件中。 如果我應該制作一個新文件,我應該將它放在 wordpress 的哪個位置?

There's not much of a point in adding javascript functions into functions.php unless you will utilize some PHP code within the javascript function or if you need the javascript defined in a specific place in the source code (in that case you might want to use a預定義的 WP 鈎子)。 對於純 javascript function,理想情況下,您應該使用某種父/子主題設置,其中您的父主題文件保持未修改,以便可以在不覆蓋任何更改的情況下更新它們,但您的子主題可以優先於特定文件(例如custom.js 或 style.css)。 每個 WordPress 設置都不同,因此很難在不了解更多詳細信息的情況下為您提供建議。

祝你好運!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM