2015-11-11 3 views
0

Я бы очень признателен за помощь в прохождении через следующий код и умножении всего на множители по своему вкусу. Спасибо заранее! Пс: использование 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)"/> 
+1

Можете ли вы объяснить, что именно вам нужно? Образец вывода будет полезен. – Samir

+0

, поэтому я хотел бы, чтобы коорды были умножены на один и тот же фактор, образец не помог бы, поскольку эти строки кода ничего не делают сами по себе. – ThijsVijver

+0

Итак, вы хотите, чтобы несколько значений в атрибуте 'coords' были обозначены числом X? –

ответ

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)); 
}); 

Попробуйте

+0

да! что сделал это, большое спасибо! – ThijsVijver

+0

Мое удовольствие :). Положите это как принятый ответ для будущих поколений. – Elentriel

+0

Ой, подождите, прежде чем идти, можете ли вы помочь мне с еще одной вещью? – ThijsVijver

0
$(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); 
    }); 
}); 
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/

+0

спасибо, это действительно полезно для понимания, мне нравится понимать мой код, прежде чем я его использую, это действительно помогло мне, спасибо еще раз! – ThijsVijver

Смежные вопросы