2010-03-30 60 views
0

我有以下JavaScript代碼,我喜歡將其轉換爲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 Javascript代碼服務器標籤之外,使其更易於閱讀。 – Aaron 2010-03-30 20:09:50

+0

哎呀! ________ – 2010-03-30 20:10:01

+4

如果你發佈你已經試過的東西會更好,所以我們可以告訴你什麼是錯的... – 2010-03-30 20:11:01

回答

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

只需注意:在最後一行中,直接轉換將覆蓋類名稱。但無論工程;) – 2010-03-30 20:22:05

+0

感謝您。它的工作完美:) – bloggerious 2010-04-03 05:31:23

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對象)不會拋出錯誤。 – 2010-03-30 21:57:32