dongyue4964 2014-06-16 09:38
浏览 57
已采纳

具有变量的Javascript函数不起作用

I have a little Problem with PHP and JavaScript. I am making a homepage with a gallery and if you click on a picture(PHP get the src address from a database) it should become greater.

Here my code:

PHP:

    echo '<h1>'."Galerie".'</h1>';
                    $r = esql("SELECT * FROM Bilder");
                    $anzahlBilder = count($r);
                    $counterForBilderInReihe = 0;
                    $counterForID = 0;
                    echo '<table>';

                    foreach($r as $bild){
                       $bildpfad = $bild['Bilderpfad']; 
                       if($counterForBilderInReihe === 0){
                           echo '<tr>';
                           echo '<td class="td"><img src="'.$bild['Bilderpfad'].'" alt="'.$bild['Bildtitel'].'" class="GalerieBilder" onclick="anzeigen('.$bildpfad.')" onMouseOver="cursorChange('.$counterForID.')" id="'.$counterForID.'"></img></td>';
                           $counterForBilderInReihe = $counterForBilderInReihe + 1;
                       }
                       else if($counterForBilderInReihe === 1){
                           echo '<td class="td"><img src="'.$bild['Bilderpfad'].'" alt="'.$bild['Bildtitel'].'" class="GalerieBilder" onclick="anzeigen('.$bildpfad.')" onMouseOver="cursorChange('.$counterForID.')" onclick="anzeigen('.$bild['Bilderpfad'].')" id="'.$counterForID.'"></img></td>';
                           $counterForBilderInReihe = $counterForBilderInReihe + 1;
                       }else if($counterForBilderInReihe === 2){
                           echo '<td class="td"><img src="'.$bild['Bilderpfad'].'" alt="'.$bild['Bildtitel'].'" class="GalerieBilder" onMouseOver="cursorChange('.$counterForID.')" onclick="anzeigen('.$bildpfad.')" id="'.$counterForID.'"></img></td>';
                           $counterForBilderInReihe = $counterForBilderInReihe + 1;
                       }else if($counterForBilderInReihe === 3){
                           echo '<td class="td"><img src="'.$bild['Bilderpfad'].'" alt="'.$bild['Bildtitel'].'" class="GalerieBilder" onMouseOver="cursorChange('.$counterForID.')" onclick="anzeigen('.$bildpfad.')" id="'.$counterForID.'"></img></td>';
                           $counterForBilderInReihe = $counterForBilderInReihe + 1;
                       }else{
                           echo '</tr>';
                           $counterForBilderInReihe = 1;
                           echo '<tr>';
                           echo '<td class="td"><img src="'.$bild['Bilderpfad'].'" alt="'.$bild['Bildtitel'].'" class="GalerieBilder" onMouseOver="cursorChange('.$counterForID.')" onclick="anzeigen('.$bildpfad.')" id="'.$counterForID.'"></img></td>';
                       }
                       $counterForID = $counterForID + 1;
                   }
                   if($counterForBilderInReihe === 0){
                       echo '</tr>';
                   }else{

                   }
                   echo '</table>';

JavaScript:

    function cursorChange(id){
console.log("cursorchange!");
document.getElementById(id).style.cursor = "pointer";}


    function anzeigen(pfad){
console.log("anzeigen");
window.alert(pfad);}

My Problem here is that the function anzeigen(pfad) doesnt work but the function cursorChange(id) works fine. The Problem specificly is that if I click on a picture it doesnt call the function anzeigen(). I can see this because of the console.log(). And the variable pfad or $bildpfad is a string value. So what is the Problem?

Thanks for helping

Rene

  • 写回答

2条回答 默认 最新

  • donglu3087 2014-06-16 09:49
    关注

    Try this....

    Use escape character for passing string value in javascript function call on click

    echo '<td class="td"><img src="'.$bild['Bilderpfad'].'" alt="'.$bild['Bildtitel'].'" class="GalerieBilder" onclick="anzeigen(\''.$bildpfad.'\')" onMouseOver="cursorChange('.$counterForID.')" id="'.$counterForID.'"></img></td>';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图