0

从自动生成的列表中选择时更改图像ID的解决方案此代码: <?php foreach(glob('pano/*.png') as $filename):?> <li><a href="javascript:chgpano()"><?php $path_parts = pathinfo ($filename); echo $path_parts['filename'], "\n";?></a> </li> <?php endforeach; unset( $filename);?>

为每个文件生成一个带有按钮的屏幕。函数chgpano:

function chgpano()
{
ima = document.getElementById("tonto");
init();
}

更改分配给变量“ima”的 img Id。“tonto”的内容需要反映所选择的图像。

以下内容不起作用,但确实显示了意图:

  function chgpano()
{
    img id="tonto" src= ('pano/'+ variable with text from button selected)
ima = document.getElementById("tonto");
init();
}

我看到了一个类似的问题,但由于不清楚而被拒绝

4

3 回答 3

0

尝试这个:

// PHP can look like
$liA = '';
foreach(glob('pano/*.png') as $f){
  $path_parts = pathinfo($f);
  $liA .= "<li><a href='javascript:chgpano($f)'>{$path_parts['filename']}></a></li>";
}
echo $liA;

// JavaScript can look like + some goodies
var doc = document; // get in the habit of declaring variables
function E(e){
  return doc.getElementById(e);
}
var tonto = E('tonto');
function chgpano(f){
  tonto.src = f;
}

我会使用外部 JavaScript,将<script>标签放在正文的底部。或者,您可以将<script>标签放入<head>并运行onload

于 2013-11-08T00:51:15.343 回答
0

正如评论中提到的,html 中 id 的目的是针对一个元素..所以我会使用class或者新的data-TAG.

要将 php 脚本也用于其他脚本/站点,我会扫描文件夹并将所有内容转换为json.(包含 png 的所有路径的简单数组)

这是一个使用 javascript 1.7(现代 html5 浏览器)的示例

用一个处理程序处理多个元素。

scanForPNGs.php

echo json_decode(glob('pano/*.png'));

索引.html

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style>
 img{width:100px;}
 img.IMA{opacity:0.3;}
</style>
<script>
var box;
function ajax(a,b,c){c=new XMLHttpRequest;c.open('GET',a);c.onload=b;c.send()};
function display(){
 box.innerHTML='<img src="'+JSON.parse(this.response).join('"><img src="')+'">';
}
function whatever(e){
 e.target.classList.toggle('IMA');// activate & deactivate
 //or
 //e.target.dataset['IMA']=e.target.dataset['IMA']=='ON'?'OFF':'ON';
}
window.onload=function(){
 box=document.getElementsByTagName('div')[0];
 box.addEventListener('click',whatever,false);
 ajax('scanForPNGs.php',display);
}
</script>
</head>
<body>
<div></div>
</body>
</html>

whatever函数内部,您可以向单击的元素添加任何属性或功能。

就我而言,我只是改变不透明度。

触发器是图像本身。

我没有测试此代码...确保使用 chrome 对其进行测试。

例子

http://jsfiddle.net/8jeDS/1/

于 2013-11-08T00:18:01.083 回答
0

谢谢伙计们。这修复了它:

<?php
            foreach(glob('pano/*.png') as $filename):$path_parts = pathinfo ($filename); $id_front = $path_parts['filename'];?>
                <li> <a onclick = "javascript:chgpano('<?php echo $id_front ?>')"><?php  echo $id_front, "\n";?></a><img id= <?php echo $id_front ?> src= <?php echo$filename ?> style="display:none"></li>
             <?php endforeach; unset( $filename);?>

如您所见,我将用于将图像 id 分配给函数参数的相同变量。

现在变量ima包含 img id

function chgpano(blah)
{
ima = document.getElementById(blah);
init();
}

我不得不将变量定义移出

  • div 否则函数参数在定义变量之前被填充。

  • 于 2013-11-09T21:42:34.513 回答