将键盘控件插入图像查看器库脚本 - javascript

我想将键盘控件向左键和向右键添加到脚本中:

  http://xenforo.com/community/threads/php-file-that-basically-functions-as-folder-image-viewer.14658/

左箭头=返回,右箭头=下一步。

每当我把函数放在?>之前,它总是返回错误。

我用这种方法

  jQuery keypress left/right navigation

$("document").keypress(function (e) { //always error here
  if(e.keyCode == 37) { // left
    //back (what here next i don't know too)
  }
  else if(e.keyCode == 39) { // right
    //next
  }
});

这是完整的脚本

<?php
// ------------------------------------------------------------------------- //
// Comic Gallery 1.2                                                        //
// ------------------------------------------------------------------------- //
// Copyright (C) 2005 Stuart Robertson                                      //
// http://www.designmeme.com/                                                //
// ------------------------------------------------------------------------- //
// This program is free software; you can redistribute it and/or modify      //
// it under the terms of the GNU General Public License as published by      //
// the Free Software Foundation; either version 2 of the License, or        //
// (at your option) any later version.                                      //
//  A summary is available at http://creativecommons.org/licenses/GPL/2.0/  //
// ------------------------------------------------------------------------- //
// Edit the code below to configure your Comic Gallery                      //
// ------------------------------------------------------------------------- //

// Your images directory, relative to the page calling this script
$imagedir="comics";

// To start at the last image use "last"
$startimage="first";

// Copyright name to display, for none use " "
$copyright=" ";

// Creative Commons license, for none use " "
// example: "http://creativecommons.org/licenses/by/2.0/"
$creativecommons=" ";

// type of divider, for none use " "
$divider="&middot;";

// show arrows, for none use 0
$arrows=1;

// show back and next, for none use 0
$backnext=1;

// show back and next, for none use 0
$firstlast=1;

// show numbers, for none use 0
$numbers=1;

// numbers per line
$linelength=10;

// navigation position, for aboe use "above"
$navplacement="below";

// ------------------------------------------------------------------------- //
// Do not edit below this line
// ------------------------------------------------------------------------- //

//    initialize variables
$pics=array();
$count=0;

// Open the directory
$comicdir=opendir($imagedir);

//    read directory into pics array
while (($file = readdir($comicdir))!==false) {
    //    filter for jpg, gif or png files...
    if (substr($file,-4) == ".jpg" || substr($file,-4) == ".gif" || substr($file,-4) == ".png" || substr($file,-4) == ".JPG" || substr($file,-4) == ".GIF" || substr($file,-4) == ".PNG"){
        $pics[$count] = $file;
        $count++;
        }
}
closedir($comicdir);

// check for the picture to view
$pic=$_GET['p'];
//    if no picture variable...
if ($pic=="") {
    if ($startimage!="last"){ $pic=1; }
    else { $pic=$count; }
}

//    sort the filenames alphabetically
sort($pics);
reset($pics);

//    determine which picture to get
for ($f=0;$f<=sizeof($pics)-1;$f++){if ($pic==$pics[$f]){$selected = $f+1;}}

//  check for javascript...
if ($pic && !preg_match("/javascript/",$pic)){

    //  get current image file
    $current=$pics[$pic-1];
    $next=$pic+1;
    if ($next > sizeof($pics)){ $next=sizeof($pics); }
    $back=$pic-1;
    if ($back < 1){ $back=1; }

    //  display image above nav
    if ($navplacement!="above"){
        if (substr($current,-4) == ".jpg" || substr($current,-4) == ".gif" || substr($current,-4) == ".png" || substr($current,-4) == ".JPG" || substr($current,-4) == ".GIF" || substr($current,-4) == ".PNG"){
                if ($pic < sizeof($pics)){
                    echo"\n<p id=\"cg_img\"><a href=\"?p=".$next."\"><img src=\"".$imagedir."/".$current."\" alt=\"Next\" /></a></p>\n";
                } else {
                echo"\n<p id=\"cg_img\"><img src=\"".$imagedir."/".$current."\" alt=\"End\" /></p>\n";
                }
            }
    }

    // display back and next
    if ($backnext != 0 || $arrows != 0){
            if (sizeof($pics) > 1){
                echo "<p id=\"cg_nav1\">";
                if ($firstlast != 0){
                    if ($pic > 1){    echo "<a href=\"?p=1\" id=\"cg_first\"><span>First</span></a>"; }
                    else { echo "<span id=\"cg_first\"><span>First</span></span>"; }
                    echo "<span class=\"cg_divider\"> ".$divider." </span>";
                }
                if ($pic > 1){
                    echo "<a href=\"?p=".$back."\" id=\"cg_back\"><span>";
                    if ($arrows != 0) { echo "&laquo; "; }
                    if ($backnext != 0) { echo "Back"; }
                    echo "</span></a>";
                } else {
                    echo "<span id=\"cg_back\"><span>";
                    if ($arrows != 0) { echo "&laquo; "; }
                    if ($backnext != 0) { echo "Back"; }
                    echo "</span></span>";
                }
                echo "<span class=\"cg_divider\"> ".$divider." </span>";
                if ($pic < sizeof($pics)){
                    echo "<a href=\"?p=".$next."\" id=\"cg_next\"><span>";
                    if ($backnext != 0) { echo "Next"; }
                    if ($arrows != 0) { echo " &raquo;"; }
                    echo "</span></a>";
                } else {
                    echo "<span id=\"cg_next\"><span>";
                    if ($backnext != 0) { echo "Next"; }
                    if ($arrows != 0) { echo " &raquo;"; }
                    echo "</span></span>";
                }
                if ($firstlast != 0){
                    echo "<span class=\"cg_divider\"> ".$divider." </span>";
                    if ($pic < sizeof($pics)){    echo "<a href=\"?p=". sizeof($pics) ."\" id=\"cg_last\"><span>Last</span></a>"; }
                    else { echo "<span id=\"cg_last\"><span>Last</span></span>"; }
                }
                echo "</p>\n";
            }
    }

    // display numbers
    if ($numbers != 0){
        if (sizeof($pics) > 1){
            //    display textlinks
            echo "<p id=\"cg_nav2\">";
            // loop over images
            for ($f=1;$f<=sizeof($pics);$f++){
                // if the link to the pic is the selected one, display a bold number and no link
                if ($pic==$f){echo "<b>".$f."</b>";}
                // otherwise display the link
                else{echo "<a href=\"?p=".$f."\">".$f."</a>";}
                // add dividers and linebreaks
                if (($f % $linelength) == 0) {
                    echo "<br />";
                }
                else {
                    if ($f!=sizeof($pics)){
                        echo "<span class=\"cg_divider\"> ".$divider." </span>";
                    }
                }
            }
            echo "</p>\n";
        }
    }

    //  display image below nav
    if ($navplacement=="above"){
        if (substr($current,-4) == ".jpg" || substr($current,-4) == ".gif" || substr($current,-4) == ".png" || substr($current,-4) == ".JPG" || substr($current,-4) == ".GIF" || substr($current,-4) == ".PNG"){
                if ($pic < sizeof($pics)){
                    echo"\n<p id=\"cg_img\"><a href=\"?p=".$next."\"><image src=\"".$imagedir."/".$current."\" alt=\"Next\" border=\"0\"></a></p>\n";
                } else {
                echo"\n<p id=\"cg_img\"><image src=\"".$imagedir."/".$current."\" alt=\"End\" /></p>\n";
                }
            }
    }

    // display copyright
    echo "<p id=\"cg_credits\">";
    if ($creativecommons != " "){ echo "<a href=\"".$creativecommons."\" title=\"Creative Commons License\">Some Rights Reserved</a> ".$divider." "; }
    else {
        if ($copyright != " "){ echo "&copy; ".$copyright." ".$divider." "; }
    }
    // If you make use of this script, be nice and keep the link back to my site :-)
    echo "Powered by <a href=\"http://designmeme.blogspot.com/2005/05/comic-gallery-script.html/\">ComicGallery</a></p>\n";
}
?>

参考方案

PHPJavaScript是两种独立的编程语言-您不能只是那样将它们混合在一起。当您将jQuery方法直接放在PHP代码内部时(例如,在'?>'之前),您是在告诉PHP运行该代码-但是PHP不知道如何运行,因为它使用的是另一种语言。

相反,请尝试将您的方法放在PHP标记(<? ... ?>)之外,或将echo放在方法之外,具体取决于页面其余部分的设置方式。

欢迎使用Stack Overflow!

在PHP文件中调用javascript函数并在加载HTML文件之后? - javascript

我需要在我的php中调用js函数,但无法正常工作。有人可以告诉我我在做什么错吗?我该如何轻松地做到这一点?谢谢!我有三个文件:  mail.php负责发送$ _POST的内容(工作正常)。我调用我的javascript函数来切换模式,具体取决于邮件是否已发送。 <? ... $response = $sendgrid->send($email);…

如何使用PHP从动态输入字段捕获数组值? - javascript

我正在编写一个在线时间跟踪网页,允许用户将学习时间输入该系统。用户将首先输入名称,然后根据日期输入学习时间。一天中可能会有多个学习时间。以下是我第一页的编码,<head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"…

用jQuery填充模式形式 - javascript

我正在将订单表从数据库绘制到datatables(jquery插件)中。我要在每笔最后一笔交易或每笔交易中增加付款。问题是,如何获取单击添加付款按钮以添加付款的行的订单ID。其次,当点击addpayment时,它会弹出一个带有字段或订单号的模态表单。我想用在td中找到的订单ID填充该字段,并使其不可编辑或隐藏,但在提交模态表单时将其发布到服务器。表格和模式表…

选择后显示输入元素 - javascript

我有一个表格,其中取决于用户的选择,输入元素是否可见。实际上,用户正在以另一种形式设置已定义的合作伙伴类型,并且如果选中该元素,则允许在该类型的合作伙伴上可见的元素类型1将显示以下元素:<input type="text" id="partner" name="partner" class=&…

JavaScript将PHP中的字符串和整数传递给函数 - javascript

我正在尝试将字符串和整数都传递到同一函数中,但是引号引起了问题。我发现错误出在echo $q->info部分,我必须在此代码上使用双引号。有人可以帮我写这个$q->info,但不能获得真正的价值吗?到目前为止,我的代码是<td><a href="javascript:add(<?php echo $q->i…