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条)

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大