简体   繁体   English

如何在PHP文件中调用jQuery函数

[英]how to call jquery function in php file

I'm working on a project and my problem is calling a jQuery function like this placing it in my php file. 我正在一个项目上,我的问题是调用这样的jQuery函数,将其放在我的php文件中。 I've tried searching a lot but I'm always coming out with an error: 我已经尝试了很多搜索,但是总是出现错误:

Function: 功能:

$(function(){
 $(".panorama-view").panorama360();
 });

Php file php文件

<?php

echo '<link rel="stylesheet" href="/css/panorama360.css" rel="stylesheet" >';
echo '<script src="/js/jquery.mousewheel.min.js" ></script>';
echo ' <script src="/js/jquery.panorama360.js" ></script>';
echo '<script>  $(function(){ $(\'.panorama-view\').panorama360(); }); </script>';
echo '</script>';

 if(isset($_POST['upload'])) {

 $image_name= $_FILES['image']['name'];
 $image_type= $_FILES['image']['type'];
 $image_size= $_FILES['image']['size'];
 $image_tmp= $_FILES['image']['tmp_name'];

 if(move_uploaded_file($image_tmp,"uploadedimg/$image_name"))
 {
     echo "<script type='text/javascript'>alert('File Uploaded!');</script>";
 }
$folder= "uploadedimg/";
if(is_dir($folder)) {

    if($handle = opendir($folder)){
        while(($file= readdir($handle)) !=false){
            if($file === '.' || $file === '..') 
                continue;
            echo '<div class="panorama round" style=" width:1200px; height:500px; padding:10px ;background-color:#444; position: relative;">';
            echo '<div class="panorama-view">';
            echo '<div class="panorama-container">';
            echo '<img src="uploadedimg/'.$file.'" data-width="4077" data-height="500" alt="Panorama" />';
            echo '</div>';
            echo '</div>';
            echo '</div>';
        }
         closedir($handle);
    }   
}     
} ?>

You get Uncaught ReferenceError: jQuery is not defined or Uncaught ReferenceError: $ is not defined error (as seen in your shared screenshot in comments under your post) because you have not included the jQuery library in your project. 由于未在项目中包含jQuery库,因此出现Uncaught ReferenceError: $ is not defined Uncaught ReferenceError: jQuery is not definedUncaught ReferenceError: $ is not defined错误(如在您的帖子下方的注释中的共享屏幕截图中所示)

Both errors are identical and pointing to the same problem as explained above. 这两个错误是相同的,并且指向如上所述的相同问题。

Note that you CANNOT use panorama360 without making use of the jQuery library as it (jQuery library) is a required dependency. 请注意,如果不使用jQuery库(jQuery库)是必需的依赖项,则不能使用Panorama360

To include the jQuery library in your project, you have two (2) main options; 要在项目中包含jQuery库,您有两个(2)主要选项; either: 要么:

You consume it from a content delivery network (CDN) by including it in your project as shown in the snippet below, 您可以通过将其包含在项目中来从内容交付网络(CDN)中使用它,如下面的代码片段所示,

<script
    src="https://code.jquery.com/jquery-3.1.1.min.js"
    integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8="
    crossorigin="anonymous"></script>

or, 要么,

downloading and referencing it directly from your stored location 直接从您的存储位置下载并引用它

<script src="/path/to/jquery-3.1.1.min.js"></script>

There is a last option which has to do with combining the earlier two mentioned above, using one as a graceful failover; 最后一个选择与上面提到的前两个结合在一起,使用一个作为正常故障转移。 here is the illustration: 这是插图:

<script
    src="https://code.jquery.com/jquery-3.1.1.min.js"
    integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8="
    crossorigin="anonymous"></script>
<script>window.jQuery || document.write('<script src="path/to/jquery-3.1.1.min.js"><\/script>')</script>

(With the above, you can make use of the jQuery library in your project by consuming it from the CDN and automatically load the version on your sever should the later fail. More details here ) (通过上述,您可以通过从CDN消耗,并自动加载在你的服务器应该在失败以后的版本利用项目中的jQuery库的更多细节在这里

It's important to note that your jQuery library declaration SHOULD occur before the referencing of your panorama360 JavaScript resources; 重要的是要注意,您的jQuery库声明应在引用您的Panorama360 JavaScript资源之前发生; either: 要么:

<script
    src="https://code.jquery.com/jquery-3.1.1.min.js"
    integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8="
    crossorigin="anonymous"></script>
<script src="/js/jquery.mousewheel.min.js"></script>
<script src="/js/jquery.panorama360.js" ></script>

Should you chose to use PHP echo() function to handle your file inclusion, do use a single quote or escape your double quotes. 如果您选择使用PHP echo()函数来处理文件包含,请使用单引号或转义双引号。 ... more details here . ...更多细节在这里

So, you should be doing something like this: 因此,您应该执行以下操作:

<?php
echo "<link rel='stylesheet' href='/css/panorama360.css'>";
echo "<script
    src='https://code.jquery.com/jquery-3.1.1.min.js'
    integrity='sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8='
    crossorigin='anonymous'></script>";
echo "<script src='/js/jquery.mousewheel.min.js'></script>";
echo "<script src='/js/jquery.panorama360.js'></script>";
echo "<script>$(function(){ $('.panorama-view').panorama360(); });</script>";

To reference your resources without making use of PHP the raw HTML way as discussed in comment below, 要在不使用PHP原始HTML方式的情况下引用您的资源,如下面的注释所述,

<?php
    // Should you want to run any PHP codes before referencing your resources,
    // you may do so here.
    // Remember: you MUST close this section as below this comments so as to
    // mark the end of your PHP code
?>
<link rel="stylesheet" href="/css/panorama360.css">
<script
    src="https://code.jquery.com/jquery-3.1.1.min.js"
    integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8="
    crossorigin="anonymous"></script>
<script src="/js/jquery.mousewheel.min.js"></script>
<script src="/js/jquery.panorama360.js"></script>
<script>$(function(){ $('.panorama-view').panorama360(); });</script>

<?php
    // Here, other PHP codes
?>

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

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