将javascript for循环转换为jquery

时间:2014-10-18 09:15:54

标签: javascript jquery

我刚开始学习jQuery,这是我的第一个项目;我有经典的JavaScript函数,我需要转换为jQuery。任何jQuery专家都可以帮助我吗?

提前谢谢。

for (var i = 0; (l_link = document.getElementsByTagName("link")[i]); i++) {
  if (l_link.getAttribute("rel")) {
    if (l_link.getAttribute("rel").indexOf("style") != -1 && l_link.getAttribute("title")) {
      l_link.disabled = true;
      if (l_link.getAttribute("title") == theme_id) {
        l_link.disabled = false;
        applied = false;
        activetheme = v_css;
      }
    }
  }
}

2 个答案:

答案 0 :(得分:1)

$('link').each(function(i, link) {
    link = $(link);
    if (link.attr('rel') && link.attr('rel').indexOf("style") != -1 && link.attr('title')) {
        if (link.attr('title') == theme_id) {
            link.attr('disabled', false);
            applied = false;
            activetheme = v_css;
        } else {
            link.attr('disabled', true);
        }
    }
});

答案 1 :(得分:0)

在JQuery中看起来像这样。

$('link').each(function(i, l_link) {   
             if (l_link.attr('rel') && l_link.attr('rel').indexOf("style") != -1 && l_link.attr('title')) {  
        if (l_link.attr('title') == theme_id) {  
            l_link.attr('disabled', false);  
            applied = false;  
            activetheme = v_css;    
        } else {  
            l_link.attr('disabled', true);  
        }  
    }  
});