将JavaScript转换为jQuery

将JavaScript转换为jQuery,jquery,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))

我有以下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';

非常感谢您的帮助。

是否转换为更简洁的JavaScript

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

转换为更简洁的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';  

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

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

我的第一个建议是将所有的
echo
'd Javascript代码移到服务器标记之外,以便于阅读。如果您发布您已经尝试过的内容,那会更好,这样我们就可以告诉您出了什么问题…@j-riilight,就像他/她尝试了任何东西一样……编辑了我想转换的部分。@Russ-看看第一个答案,“可读”是一个相对的术语:)我的第一个建议是将所有的
echo
'd Javascript代码移到服务器标记之外,以使其更易于阅读。如果您发布您已经尝试过的内容,会更好,所以我们可以告诉你出了什么问题…@j-RiiLight,就像他/她尝试了任何东西一样…编辑了我想要转换的部分。@Russ-看看第一个答案,“可读”是一个相对的术语:)只是一个注释:在最后一行中,直接转换将覆盖类名。但不管什么有效;)请注意:在最后一行中,直接转换将覆盖类名。但不管什么有效;)无需检查返回的jQuery对象的长度-对不匹配的jQuery对象(即“空”jQuery对象)调用方法不会引发错误。无需检查返回的jQuery对象的长度-对不匹配的jQuery对象(即“空”jQuery对象)调用方法不会引发错误。