简体   繁体   English

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

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

I have progress.js file which has the following code我有 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;
            });
        });

I have to call that function in my php file.我必须在我的 php 文件中调用 function。 How could i make it right?我怎么能做对呢? And I include all necessary css in my project我在我的项目中包含了所有必要的 css

you can put your jquery code in a seperate file and then include this in your other page using include您可以将您的 jquery 代码放在一个单独的文件中,然后使用 include 将其包含在您的其他页面中

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

Well there is a great solution.那么有一个很好的解决方案。 Instead of using it in document.ready work like this而不是像这样在 document.ready 中使用它

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

And you script should contain this你的脚本应该包含这个

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

if you are calling the other php file in this file this function will be available for the new php file too.如果您在此文件中调用其他 php 文件,则此 function 也可用于新的 php 文件。 The other solution is that copy the code in the other php file另一种解决方案是复制其他 php 文件中的代码

I have one header.php file, In header.php i insert some and in head tag and also I include body tag.我有一个 header.php 文件,在 header.php 中,我在 head 标签中插入了一些,并且还包含了 body 标签。 But in webpage.php file, I have one text area and i apply some jquery to get progress bar dynamically.但是在 webpage.php 文件中,我有一个文本区域,我应用一些 jquery 来动态获取进度条。 So I have to call $document.ready function in that webpage.php.所以我必须在该网页中调用 $document.ready function.php。 But The problem is, I have lot of $document.但问题是,我有很多 $document。 ready function in header.php which i included in webpage.php.在 header.php 中准备好 function,我将其包含在 webpage.php 中。 And One more thing I have another head tag in webpage.php which is used for getting JQGrid还有一件事我在网页中有另一个头标签。php 用于获取 JQGrid

As I understand you have multiple of document.ready block already, not you want to call a specific block only.据我了解,您已经有多个 document.ready 块,而不是只想调用特定块。

You should change from你应该改变从

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

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

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

to function codeblock1(){ //code for block 1 }到 function codeblock1(){ // 块 1 的代码 }

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

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

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

so now you can call any block you want in o ther script所以现在你可以在其他脚本中调用你想要的任何块

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

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