簡體   English   中英

Javascript: 在另一個 php 文件中調用一個 js 文件的 document.ready() function

[英]Javascript : calling document.ready() function of one js file in another php file

我有 progress.js 文件,其中包含以下代碼

            $('#text_area_input').keyup(function()
            {
                var text_area_box =$(this).val();//Get the values in the textarea
                var max_numb_of_words = 160;//Set the Maximum Number of characters
                var main = text_area_box.length*100;//Multiply the lenght on words x 100
                var value= (main / max_numb_of_words);//Divide it by the Max numb of words previously declared
                var count= max_numb_of_words - text_area_box.length;//Get Count of remaining characters
                if(text_area_box.length <= max_numb_of_words)
                {
                    $('#progressbar').css('background-color','#5fbbde');//Set the background of the progressbar to blue
                    $('#count').html(count);//Output the count variable previously calculated into the div with id= count
                    $('#progressbar').animate(//Increase the width of the css property 'width'
                    {
                        'width': value+'%'
                    }, 1);//Increase the
                }
                else
                {
                    $('#progressbar').css('background-color','yellow');
                    //If More words is typed into the textarea than the specified limit ,
                    //Change the progress bar from blue to yellow
                    var remove_excess_characters =text_area_box.substr(0,max_numb_of_words);
                    $('#text_area_input').val(remove_excess_characters);
                    //Remove the excess words using substring
                }
                return false;
            });
        });

我必須在我的 php 文件中調用 function。 我怎么能做對呢? 我在我的項目中包含了所有必要的 css

您可以將您的 jquery 代碼放在一個單獨的文件中,然后使用 include 將其包含在您的其他頁面中

http://php.net/manual/en/function.include.php

那么有一個很好的解決方案。 而不是像這樣在 document.ready 中使用它

<input type = 'button' onkeyup = 'my_function();' id = 'text_area_input'>

你的腳本應該包含這個

 <script type = 'text/javascript'>
 function my_function(){
 // you code here
 }
 <script>

如果您在此文件中調用其他 php 文件,則此 function 也可用於新的 php 文件。 另一種解決方案是復制其他 php 文件中的代碼

我有一個 header.php 文件,在 header.php 中,我在 head 標簽中插入了一些,並且還包含了 body 標簽。 但是在 webpage.php 文件中,我有一個文本區域,我應用一些 jquery 來動態獲取進度條。 所以我必須在該網頁中調用 $document.ready function.php。 但問題是,我有很多 $document。 在 header.php 中准備好 function,我將其包含在 webpage.php 中。 還有一件事我在網頁中有另一個頭標簽。php 用於獲取 JQGrid

據我了解,您已經有多個 document.ready 塊,而不是只想調用特定塊。

你應該改變從

$(document).ready(
    //code for block 1
)

$(document).ready(
    //code for block 2
)

$(document).ready(
    //code for block 3
)

到 function codeblock1(){ // 塊 1 的代碼 }

function codeblock2(){
    //code for block 2
}

function codeblock3(){
    //code for block 3
}

$(document).ready(
    codeblock1();
    codeblock2();
    ///
)

所以現在你可以在其他腳本中調用你想要的任何塊

暫無
暫無

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

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