dsagzmosl32217092 2015-04-17 10:28
浏览 50
已采纳

Jquery 1 id具有更多图像并更改所有图像宽度/高度/位置

I have about 10-15 images with only one id: "buildings": When the page loads up, I'd like to change all the images width/height/position left and top, but if I echo the images with a foreach, and use the script, the script change size only for the first image. Why, and how can I fix it?

This is the script:

$( document ).ready(function() {
        var width = window.outerWidth;
        var height = window.outerHeight;
        var alapw = 1680, alaph = 1000;

        var aranyW = width/alapw;
        var aranyH = height/alaph;


        $( "#map").css( "width", parseInt($( "#map").css( "width")) * aranyW);
        $( "#map").css( "height", parseInt($( "#map").css( "height")) * aranyH);


        $( "#buildings").css( "width", parseInt($( "#buildings").css( "width")) * aranyW);
        $( "#buildings").css( "height", parseInt($( "#buildings").css( "height")) * aranyH);
        $( "#buildings").css( "left", parseInt($( "#buildings").css( "left")) * aranyW);
        $( "#buildings").css( "top", parseInt($( "#buildings").css( "top")) * aranyH);
    });

    function epuletModosul( a, b ) {
        location.href="../views/epuletmodosul.php?id=" + b;
    }

And this is the php (images):

foreach($epuletek[0] as $key=>$value)
{
    foreach( $osszEpulet as $kulcs=>$ertek )
    {
        $plit = explode("_",$key);
        if( $plit[0] == $ertek[0] )
        {
            if( $value == 0 )
            {
                echo "<img id=\"buildings\" name=\"$ertek[0]\" src=\"../resources/images/buildings/sand.png\" width=\"77\" height=\"77\" onclick=\"epuletModosul(0,-1)\"  style=\" position: absolute; left: $ertek[1]; top: $ertek[2];\">";

            }
            else if( $value == 1 )
            {
                echo "<img id=\"buildings\" name=\"$ertek[0]\" src=\"../resources/images/buildings/$ertek[0].png\" width=\"77\" height=\"77\" onclick=\"epuletModosul(1,$kulcs)\"  style=\" position: absolute; left: $ertek[1]; top: $ertek[2];\">";
            }
        }
    }
}
  • 写回答

1条回答 默认 最新

  • dongzhou1901 2015-04-17 10:45
    关注

    As Satpal stated id must be unique, however you can use the class attribute.

    PHP:

    foreach($epuletek[0] as $key=>$value)
    {
        foreach( $osszEpulet as $kulcs=>$ertek )
        {
            $plit = explode("_",$key);
            if( $plit[0] == $ertek[0] )
            {
                if( $value == 0 )
                {
                    echo "<img class=\"buildings\" name=\"$ertek[0]\" src=\"../resources/images/buildings/sand.png\" width=\"77\" height=\"77\" onclick=\"epuletModosul(0,-1)\"  style=\" position: absolute; left: $ertek[1]; top: $ertek[2];\">";
    
                }
                else if( $value == 1 )
                {
                    echo "<img class=\"buildings\" name=\"$ertek[0]\" src=\"../resources/images/buildings/$ertek[0].png\" width=\"77\" height=\"77\" onclick=\"epuletModosul(1,$kulcs)\"  style=\" position: absolute; left: $ertek[1]; top: $ertek[2];\">";
                }
            }
        }
    }
    

    JS:

    $( document ).ready(function() {
        var width = window.outerWidth;
        var height = window.outerHeight;
        var alapw = 1680, alaph = 1000;
    
        var aranyW = width/alapw;
        var aranyH = height/alaph;
    
    
        $( "#map").css( "width", parseInt($( "#map").css( "width")) * aranyW);
        $( "#map").css( "height", parseInt($( "#map").css( "height")) * aranyH);
    
        $( ".buildings").each(function(){
            var cssValue={
                            width: parseInt($(this).css( "width")) * aranyW,
                            heigh: parseInt($(this).css( "height")) * aranyH,
                            left: parseInt($(this).css( "left")) * aranyW,
                            top: parseInt($(this).css( "top")) * aranyH
                         };
            $(this).css(cssValue);
        });
    });
    
    function epuletModosul( a, b ) {
        location.href="../views/epuletmodosul.php?id=" + b;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥20 为什么我写出来的绘图程序是这样的,有没有lao哥改一下
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥200 关于#c++#的问题,请各位专家解答!网站的邀请码
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥15 绘制多分类任务的roc曲线时只画出了一类的roc,其它的auc显示为nan
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?