2016-12-17 5 views
1

변수 펠릿 (원 svg)을 pelClass 클래스에 할당하려고합니다. 어떤 이유로 그것은 배정되지 않고 많은 문제를 야기하고 있습니다. 도와주세요!SVG 요소에 클래스를 할당하는 방법

function positionPellet() 
{ 
    constantCount = 1; 

    while(pelletCount < constantCount*3) 
    { 
    var pellet = document.createElementNS(xmlns, 'circle'); 
    pellet.setAttribute('cx', Math.random() * window.innerWidth); 
    pellet.setAttribute('cy', Math.random() * window.innerHeight); 
    pellet.setAttribute('r' , 10); 
    pellet.className = "pelClass"; //Pellet Class Adding 
    pelletCount++; 
    pelletList = new Array(pelletCount); 
    pelletList.push(pellet); 
    svg.appendChild(pellet); 
    } 
} 
+0

pelletList = 새 Array (HTML와 동일하게 작동)의 setAttribute (pelletCount)를 사용할 수 있습니다; ? 정말로 새로운 배열을 만들고 싶습니까? 어쩌면이 문은 루프 앞에 실행되어야합니다. – user2182349

+0

'pellet.class = "pelClass";'시도 했습니까? 왜 당신의 문제를 "클래스에 한개의 변수를 할당하려고"한다고 묘사 했습니까? SVG 요소에 클래스를 할당하려고하면 안됩니까? –

+0

제대로 pelletCount, xmlns 및 svg 값을 할당하면 코드가 제대로 작동합니다. 뭐가 문제 야? – Deep

답변

1

SVG에서 구문은

pellet.className.baseVal = "pelClass"; //Pellet Class Adding 

거나 또는 당신

pellet.setAttribute('class', "pelClass"); //Pellet Class Adding 
+0

그것은 내 문제를 거의 수정했습니다. 충돌을 검사하기 위해 클래스에 추가해야하는 것을 보았습니다.이 클래스를 실행하고 해당 클래스가 원과 충돌했을 때 해당 원이 적절한 클래스 이름과 연관되어 있음을 보여주지 못했습니다. –

관련 문제