Javascript 循环通过某个东西,然后乘上所有东西
我真的很想得到一些帮助,通过下面的代码循环并将所有内容乘以我喜欢的因子。 提前谢谢你! Ps:使用jquery也可以这样做Javascript 循环通过某个东西,然后乘上所有东西,javascript,jquery,html,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,3
<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)"/>
尝试一下这个虽然当前的答案确实解决了问题,但我有时发现,当您稍后回到代码时,一个更扩展、更分散的解决方案通常更容易遵循
$(function(){
var fixedvar=5;
$('area').each(function(){
var arr=$(this).attr('coords').split(',');
var sum=1;
$.each(arr,function(index,value){
sum+=fixedvar*value;
});
console.log(sum);
});
});
这是我的建议
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);
这里有一把小提琴可以看到它在工作:你能解释一下你到底需要什么吗?一个示例输出会很有帮助。因此我希望所有坐标都乘以相同的因子,但一个示例并没有真正的帮助,因为这些代码行本身并没有任何作用。因此,您希望将
coords
属性中的每个值乘以一个数字X?我很高兴:)。在你离开之前,你能再帮我一件事吗?如果是关于这场火灾的,如果是不同的问题,打开不同的帖子,在那里见你好吧,这有点,这就是我使用你的方法的原因。谢谢,这对理解非常有用,我喜欢在使用之前理解我的代码,这真的帮助了我,再次感谢!
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);