2010-03-30 6 views
0

jQuery로 변환하고 싶지만 행운이없는 다음 JavaScript 코드가 있습니다.자바 스크립트를 jQuery로 변환

var mySplitResult = xvotesString[htmlid].split('~'); 
target3 = document.getElementById ('xvote-' + htmlid); 
target3.style.width = mySplitResult[0] + 'px'; 
if (target4 = document.getElementById ('mnma-' + htmlid)); 
    target4.innerHTML = mySplitResult[1];; 
if (target5 = document.getElementById ('mnmb-' + htmlid)); 
    target5.innerHTML = mySplitResult[2];; 
if (target6 = document.getElementById ('mnmc-' + htmlid)); 
    target6.style.display='none';; 
if (target6 = document.getElementById ('mnmd-' + htmlid)); 
    target6.style.display='block'; 
target7 = document.getElementById ('xvotes-' + htmlid); 
target7.className = 'star-rating-noh'; 

모든 도움을 주시면 감사하겠습니다.

+1

내 첫 추천을 읽기 쉽게하기 위해 서버 태그 외부에서'echo''d 자바 스크립트 코드를 모두 이동하는 것입니다. – Aaron

+0

Yikes! ________ –

+4

당신이 이미 시도한 것을 게시하면 우리는 무엇이 잘못되었는지 말할 수 있기 때문에 더 좋을 것입니다 ... –

답변

4
var mySplitResult = xvotesString[htmlid].split('~'); 

$('#xvote-' + htmlid).width(mySplitResult[0] + 'px'); 
$('#mnma-' + htmlid).html(mySplitResult[1]); 
$('#mnmb-' + htmlid).html(mySplitResult[2]); 
$('#mnmc-' + htmlid).hide(); 
$('#mnmd-' + htmlid).show(); 
$('#xvotes-' + htmlid).addClass('star-rating-noh'); 
+0

참고 : 마지막 줄에는 a 직접 변환은 클래스 이름을 무시하는 것입니다. 그러나 무엇이든간에;) –

+0

감사합니다. 그것은 완벽하게 작동했습니다. – bloggerious

0

더 간결한 JavaScript로 변환 하시겠습니까?

var results = xvotesString[htmlid].split('~'), 
    elem = function elem(prefix) { 
     return document.getElementById(prefix + htmlid); 
    } 
if(var t3 = elem('xvote-')) 
    t3.style.width = results[0] + 'px'; 
if(var t4 = elem('mnma-')) 
    t4.innerHTML = results[1]; 
if(var t5 = elem('mnma-')) 
    t5.innerHTML = results[2]; 
if(var t6 = elem('mnmc-')) 
    t6.style.display='none'; 
if(t6 = elem('mnmd-')) 
    t6.style.display='block'; 
if(var t7 = elem('xvotes-')) 
    t7.className += ' star-rating-noh'; 

0
var mySplitResult = xvotesString[htmlid].split('~'); 

$('#xvote-' + htmlid).css('width', mySplitResult[0] + 'px'); 

if ($('#mnma-' + htmlid).length > 0); 
    $('#mnma-' + htmlid).html(mySplitResult[1]); 

if ($('#mnmb-' + htmlid).length > 0); 
    $('#mnmb-' + htmlid).html(mySplitResult[2]); 

if ($('#mnmc-' + htmlid).length > 0); 
    $('#mnmc-' + htmlid).css('display', 'none'); 

if ($('#mnmd-' + htmlid).length > 0); 
    $('#mnmd-' + htmlid).css('display', 'block'); 

$('#xvotes-' + htmlid).addClass('star-rating-noh'); 

또는

var mySplitResult = xvotesString[htmlid].split('~'), 
     target3 = $('#xvote-' + htmlid), 
     target4 = $('#mnma-' + htmlid), 
     target5 = $('#mnmb-' + htmlid), 
     target6 = $('#mnmc-' + htmlid), 
     target0 = $('#mnmd-' + htmlid), //replace target6 
     target7 = $('#xvotes-' + htmlid); 

$(target3).css('width', mySplitResult[0] + 'px'); 

if ($(target4).length > 0); 
    $(target4).html(mySplitResult[1]); 

if ($(target5).length > 0); 
    $(target5).html(mySplitResult[2]); 

if ($(target6).length > 0); 
    $(target6).css('display', 'none'); 

if ($(target0).length > 0); 
    $(target0).css('display', 'block'); 

$(target7).addClass('star-rating-noh'); 
+0

반환 된 jQuery 객체의 길이를 확인할 필요가 없습니다. 일치하지 않는 jQuery 객체 (즉, "빈"jQuery 객체)에서 메소드를 호출하면 오류가 발생하지 않습니다. –

관련 문제