循环通过某些东西并繁衍一切

时间:2015-11-11 18:40:45

标签: javascript jquery html

我非常感谢您循环使用以下代码并将所有内容与我喜欢的因素相乘。 先感谢您! Ps:使用jquery来做也是一种选择。

        <area shape="poly" coords=" 97,125,168,125,202,186,168,246, 97,246, 63,187"  onClick="Tile(0,0)"/>
        <area shape="poly" coords=" 97,246,168,246,202,307,168,368, 97,368, 63,306"  onClick="Tile(0,1)"/>
        <area shape="poly" coords=" 97,368,168,368,202,428,168,490, 97,489, 63,429"  onClick="Tile(0,2)"/>
        <area shape="poly" coords="201, 64,273, 64,307,125,273,187,202,186,168,125"  onClick="Tile(1,0)"/>
        <area shape="poly" coords="201,186,273,186,307,246,273,307,202,307,168,246"  onClick="Tile(1,1)"/>
        <area shape="poly" coords="201,307,273,307,307,368,273,428,202,428,168,368"  onClick="Tile(1,2)"/>
        <area shape="poly" coords="201,428,273,428,307,489,273,549,202,551,168,490"  onClick="Tile(1,3)"/>
        <area shape="poly" coords="306,125,378,125,411,186,378,246,306,246,273,186"  onClick="Tile(2,0)"/>
        <area shape="poly" coords="306,246,378,246,411,307,378,368,306,368,273,308"  onClick="Tile(2,1)"/>
        <area shape="poly" coords="306,368,378,368,411,429,378,489,306,489,273,428"  onClick="Tile(2,2)"/>
        <area shape="poly" coords="412, 64,482, 64,517,125,482,186,412,186,378,125"  onClick="Tile(3,0)"/>
        <area shape="poly" coords="412,186,482,186,517,247,482,308,412,307,378,247"  onClick="Tile(3,1)"/>
        <area shape="poly" coords="412,307,482,307,517,369,482,428,412,428,378,368"  onClick="Tile(3,2)"/>
        <area shape="poly" coords="412,428,482,428,517,489,482,550,412,550,378,490"  onClick="Tile(3,3)"/>
        <area shape="poly" coords="515,126,587,125,621,186,587,246,516,246,482,186"  onClick="Tile(4,0)"/>
        <area shape="poly" coords="515,246,587,246,621,307,587,368,516,368,482,308"  onClick="Tile(4,1)"/>
        <area shape="poly" coords="515,368,587,368,621,429,587,488,516,490,482,428"  onClick="Tile(4,2)"/> 

3 个答案:

答案 0 :(得分:5)

虽然目前的答案可以解决问题,但我有时会发现,当您稍后再回到代码时,更容易遵循更加扩展和拆分的解决方案。

这是我的建议

function multiplyByFactor(list, factor) {
    return list.map(function (itm) {
        return itm * factor;
    });
}

function stringArrayToNumArray(list) {
    return list.map(function (str) {
        return parseInt(str);
    });
}

function multiplyCoordinates(elements) {
    elements.each(function () {
        var el = $(this);
        //extract the current coordinates from the element
        var strCoords = el.attr("coords").split(",");
        //turn them into Numbers, to avoid any weird Javascript math
        var numCoords = stringArrayToNumArray(strCoords);
        //Multiply each by our factor
        var inflatedCoords = multiplyByFactor(numCoords, 5);
        //Set replace the attribute
        el.attr("coords", inflatedCoords.join(","));
    });
}

var areaElements = $("area");
multiplyCoordinates(areaElements);

这是一个让它看起来有效的小提琴:https://jsfiddle.net/tm4bxgce/

答案 1 :(得分:3)

var x = 5;
jQuery("area").each(function(){
 var coords = jQuery(this).attr("coords").split(",");
 var output = "";
 jQuery.each(coords , function(index,element){
  output += (parseInt(element) * x)+",";
 });
 jQuery(this).attr("coords",output.slice(0, - 1));
});

试试这个

答案 2 :(得分:0)

CAST(... TO TIMEZONE)