我有4个格,并在每个鼠标悬停,我要src的img改变.
我必须从PHP获取图像.
如何在我的jQquery脚本中使用我在PHP中获得的数组?
我知道只有4,我可以在javascript中设置数组并放置每个图像名称,但这不是我正在寻找的功能.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<style>
div{
height:100px;
width:100px;
display:inline-block;
background-color:blue;
}
#my_image{
height:100px;
width:100px;
display:inline-block;
}
</style>
<?php
$images = scandir("images", 1);
?>
<script>
$(document).ready(function(){
$(div).mouseover(function(){
$("#my_image").attr("src", /*my php array*/);
});
});
</script>
</head>
<body>
<div></div>
<div></div>
<div></div>
<div></div>
<img id="my_image" />
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
var images = <?php echo json_encode($images); ?>;
Run Code Online (Sandbox Code Playgroud)
然后你有一个images可以在JavaScript代码中使用的数组.尽管如此,在评论的位置使用它是没有意义的.您无法将数组分配给图像的src.
| 归档时间: |
|
| 查看次数: |
335 次 |
| 最近记录: |