2017-05-18 6 views
-3

'box-tip'[0] 색인을 호출 할 때 실패한 스크립트가 있습니다. 이 스크립트를 개선/수정하거나 더 이상 방탄을 일으켜 파손되지 않도록하는 방법이 있습니까? 모든 제안을 환영합니다, 내 코드를 아래에서보십시오. 박스 팁 특히이 스크립트를 '방탄'하는 방법

var map = {}; 
 

 
var site = util.getCookie('CTCountry').toLowerCase(); 
 
if (site === 'gb' || site === 'us' || site === 'xbr' || site === 'eu') { 
 
    map = { 
 
    '14.5': 33, 
 
    '15': 33, 
 
    '15.5': 34, 
 
    '16': 35, 
 
    '16.5': 35, 
 
    '17': 35, 
 
    '17.5': 36, 
 
    '18': 36, 
 
    '19': 37, 
 
    '20': 37 
 
    }; 
 
} else { 
 
    map = { 
 
    '37': 84, 
 
    '38': 84, 
 
    '39': 86, 
 
    '41': 86, 
 
    '42': 89, 
 
    '43': 89, 
 
    '44': 91, 
 
    '46': 91, 
 
    '48': 94, 
 
    '50': 94 
 
    }; 
 
} 
 

 
function applyRecommendedSleeveLength(selectedVal) { 
 
    if (selectedVal !== undefined) { 
 
    var recommendedVal = map[selectedVal.trim()]; 
 
    var selected = $('.attribute__swatch--selected:first div').text().trim(); 
 
    if (recommendedVal === null || recommendedVal === undefined) { 
 
     selectedVal = $('.attribute__swatch--selected:first div').text().trim(); 
 
     recommendedVal = map[selectedVal.trim()]; 
 
    } 
 
    var sleevSwatches = document.querySelectorAll('[class*="attribute__swatch--length-"] div'); 
 
    sleevSwatches.forEach(function(swatch, i) { 
 
     $('showBorder').removeClass('info'); 
 
     swatch.classList.remove('showBorder'); 
 
     $('.box-tip').hide(); 
 
    }); 
 

 
    if (selected === null || selected === '' || selected === undefined) return; 
 

 
    var recommendedLis = document.querySelectorAll('[class*="attribute__swatch--length-' + recommendedVal + '"] div'); 
 
    recommendedLis.forEach(function(recommendedLi, i) { 
 
     if (recommendedLi !== null && recommendedLi !== undefined) { 
 
     recommendedLi.classList.add('showBorder'); 
 
     $('.box-tip').show(); 
 
     var currentPosition = $('.showBorder').parent().position().left; 
 
     var info = document.getElementsByClassName('box-tip')[0]; 
 
     if (info !== null && info !== undefined) { 
 
      info.style.paddingLeft = currentPosition + -75 + 'px'; 
 
     } 
 
     } 
 
    }); 
 
    } 
 
} 
 

 
(function() { 
 
    if (typeof NodeList.prototype.forEach === "function") return false; 
 
    NodeList.prototype.forEach = Array.prototype.forEach; 
 
})();

+1

어. 스 니펫은 작동하지 않습니다 (HTML이 아니기 때문에 일부 서버 측 템플리트 것입니다). 따라서 페이지가 어떻게 보이는지 알 수있는 방법이 아니며 매우 구체적인 질문이나 오류 메시지가 아닙니다. 렌더링 된 html을 제공하면 누군가가 "Run code snippet"을 클릭했을 때 문제가 있다는 것을 알게되면 멋질 것입니다. – James

+0

@James 나는 질문을 편집했다. 미안해 내가 더 명확하게 스크립트를 만들 수 있기를 바란다. 그래서 무언가가 실행되지 않으면 예를 들어 상자 팁이 발견되지 않으면 전체 백 옵션이있다. 페이지가 깨지지 않는다. – Olivbelle

답변

1

:

var info = document.getElementsByClassName('box-tip')[0]; 

당신도 해당 모음의 첫 번째 요소를 읽기를 강요하기 때문에 class='box-tip'과에 요소가없는 경우이 중단됩니다 아무 것도 없을지라도. 신속하게 수리 할 수 ​​있습니다 :

var collection = document.getElementsByClassName('box-tip'); 
var info = collection.length ? collection[0] : false; 
// if there were no elements in the collection info = false; 

if (info) { 
    info.style.paddingLeft = currentPosition + -75 + 'px' ; 
}