开发者

How to use jQuery within PHP

开发者 https://www.devze.com 2023-01-28 15:08 出处:网络
I\'m using a php tree browser that I got from http://www.lateralcode.com/directory-trees-with-php-and-jquery/

I'm using a php tree browser that I got from http://www.lateralcode.com/directory-trees-with-php-and-jquery/

<?php
 $path = ".";

 function createDir($path = '/')
 { 
  if ($handle = opendir($path)) 
  {
   echo "<ul>";

   while (false !== ($file = readdir($handle))) 
   {
    if (is_dir($path.$file) && $file != '.' && $file !='..')
     printSubDir($file, $path, $queue);
    else if ($file != '.' && $file !='..')
     $queue[] = $file;
   }

   printQueue($queue, $path);
   echo "</ul>";
  }
 }

 开发者_Python百科function printQueue($queue, $path)
 {
  foreach ($queue as $file) 
  {
   printFile($file, $path);
  } 
 }

 function printFile($file, $path)
 { 
  echo "<li><a style=\"cursor:pointer\" ".
       "onClick=\"parent.right.document.getElementById('file_path').value='".
       "$file_path'\">$file_path</a></li>";
 }

 function printSubDir($dir, $path)
 {
  echo "<li><span class=\"toggle\">$dir</span>";
  createDir($path.$dir."/");

 ?>

 **<script language="javascript">
   $(document).click(function(e) { 
   if (e.button == 0) {
    // was the left button
    alert('clicked'); 
   }
  });
  </script>**

 <?php 
  echo "</li>";

However, I want to detect a right-click that is pressed inbetween, so that I can trigger an event. However, I can't seem to get the jquery code to work within my PHP. The reason being, that I don't get an alert popup when I execute the code ... Ideally, I want to have this included within my php code, since I need to use my php variable $file_path


PHP can only process code and send output to the browser. jQuery runs strictly in the browser and will either be executed when the DOM loads it (ie, when PHP sends it and the browser receives/processes it) or when the document has finished loading (usually preferable). To do the latter use this:

$(document).ready(function() {
   $(document).click(function(e) { 
   if (e.button == 0) {
    // was the left button
    alert('clicked'); 
   }
  });
});

I'm making the assumption your code is correct because it isn't extremely clear what you're trying to do. But that might shore up some of your problems.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号