在php脚本中使用javascript

时间:2014-02-12 19:57:16

标签: javascript php html onmouseover

我在这里有一些代码,用于在鼠标悬停时更改或交换图像。这是javascript代码:

<script type="text/javascript" language="Javascript">

var imgdir = "category_icons/";

blank_icont = new Image;
blank_icont.src= imgdir+"blank_text.png";
vieworder_icon = new Image;
vieworder_icon.src = imgdir+"vieworder.png";
vieworder_icono = new Image;
vieworder_icono.src= imgdir+"vieworder2.png";
//vieworder_icont = new Image;
//vieworder_icont.src= imgdir+"vieworder.png";

function changeImages(name) { 
document.images[name].src = eval(name + '_icono.src');
document.images.icon_text.src  = eval(name + '_icont.src');
}

function changeImages2(name){ 
document.images[name].src = eval(name + '_icon.src');
document.images.icon_text.src  = eval('blank_icont.src');
    }
</script>

在我的PHP脚本中,我有一些代码:

$dynamiclist = '<table align="center" width="60%" border="0" cellspacing="5" cellpadding="8">
                        <tr height="10"><hr style="width: 60%;"></tr>
                        <tr>
                            <td width="30% valign="top" align="left"><img style="border: #66cc33 5px solid;" src="./menupictures/'. $picturepath . '" height="140" width="140" border="1"/></td>
                            <td width="40%" valign="top" align="left"> ' . $name . ' <br /> $' . $price . ' <br /><br /><a href="moreinfo.php?id=' . $id .'">More Info</a></td>
                            <td width="30% valign="top" align="left"><a onmouseover="changeImages("vieworder")" onmouseout="changeImages2("vieworder")" href="customize.php?id=' . $id .'"><img id="vieworder" src="category_icons
                                                                        /vieworder.png" alt="" name="vieworder" border="0"></a></td>

您会注意到我尝试使用函数changeImages的位置。是不行,因为我不能用这种方式使用javascript?或者我的报价不正确?我试过通过移动报价来改变一些事情,但没有任何工作。

这是完整的代码:

<html>
<head>

<script type="text/javascript" language="Javascript">

var imgdir = "category_icons/";

blank_icont = new Image;
blank_icont.src= imgdir+"blank_text.png";
vieworder_icon = new Image;
vieworder_icon.src = imgdir+"vieworder.png";
vieworder_icono = new Image;
vieworder_icono.src= imgdir+"vieworder2.png";
//vieworder_icont = new Image;
//vieworder_icont.src= imgdir+"vieworder.png";

function changeImages(name) { 
document.images[name].src = eval(name + '_icono.src');
document.images.icon_text.src  = eval(name + '_icont.src');
}

function changeImages2(name){ 
document.images[name].src = eval(name + '_icon.src');
document.images.icon_text.src  = eval('blank_icont.src');
    }
</script>

</head>
<body>

<!-- This is where the table section starts -->
<table><tr height="50">&nbsp;</tr></table> 
</body>
</html>

<?php
/* ----------------------------------- CONNECTING AND QUERYING THE DATABASE -----------------------*/

require("database.php"); //connect to the database

$start = (isset($_GET['start']) ? (int)$_GET['start'] : 0); //setting the get function for pagnation

$result = mysqli_query($con,"SELECT * FROM menuitem WHERE type='kids'LIMIT $start, 3"); //mysql statement. first argument $starat specifies the offset of the first row to return, and the second specifies the maximum number of rows to return 
    if (!$result) {
        printf("Error: %s\n", mysqli_error($con));// Displays the error that mysql will generate if syntax is not correct.
        exit();
    }


echo '<table><tr height="20"></tr></table>

        <table height="50" width="60%" align="center" border="0" cellpadding="0" cellspacing="0" align="center">
            <tr>
                <td width="300" align="left"><font style="font-weight:bold;">Menu Items</font></td>
                <td width="400" align="left"><font style="font-weight:bold;">Menu Information</font></td>
                <td width="300" align="left"><font style="font-weight:bold;">Add to Order</font></td>
            </tr>
        </table>';
/* ----------------------------------- PULLING IN THE DATA -----------------------*/

//DYNAMIC PHP PULLING IN THE DATA AND SPITTING OUT THE RESULTS
while($row = mysqli_fetch_array($result))
{
    $id = $row['id'];
    $description = $row['description'];
    $picturepath = $row['picturepath'];
    $name = $row['name'];
    $price = $row['price'];

    $dynamiclist = '<table align="center" width="60%" border="0" cellspacing="5" cellpadding="8">
                        <tr height="10"><hr style="width: 60%;"></tr>
                        <tr>
                            <td width="30% valign="top" align="left"><img style="border: #66cc33 5px solid;" src="./menupictures/'. $picturepath . '" height="140" width="140" border="1"/></td>
                            <td width="40%" valign="top" align="left"> ' . $name . ' <br /> $' . $price . ' <br /><br /><a href="moreinfo.php?id=' . $id .'">More Info</a></td>
                            <td width="30% valign="top" align="left"><a onmouseover="changeImages(&quot;vieworder&quot;)" onmouseout="changeImages2(&quot;vieworder&quot;)" href="customize.php?id=' . $id .'"><img id="vieworder" src="category_icons
                                                                        /vieworder.png" alt="" name="vieworder" border="0"></a></td>
                        </tr>
                    </table>';
    echo $dynamiclist;
    }

3 个答案:

答案 0 :(得分:1)

我一眼就认为你的报价有问题。试试这个:

onmouseover="changeImages(\"vieworder\")" onmouseout="changeImages2(\"vieworder\")"

答案 1 :(得分:1)

雅各布是正确的,报价是问题,但解决方案是不同的:

onmouseover="changeImages(&quot;vieworder&quot;)"

在转义内容时考虑上下文始终很重要。

答案 2 :(得分:0)

改变这个:

 onmouseover="changeImages(\'vieworder\')" onmouseout="changeImages2(\'vieworder\')"