繁体   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