2012-03-15 57 views
0
ApplyClickableLinkToClass = function(selectedElements) { 
    // Go through each of the passed in selections and try to apply a link to them 
    $.each(selectedElements, function() { 
     var linkElement = $("a:first:not(.do-not-apply-clickable-link)", $(this)); 
     var link = linkElement.attr("href"); 

     if (!IsNullEmptyOrUndefined(link)) { 
      $(this).click(function(firstLink) { 
       var divToLink = firstLink; 
       return function() { 
        $(divToLink).unbind('click'); 
        if (divToLink.attr("target") != "_blank") { 
         window.location = link; 
         return false; 
        } 
       }; 
      } (linkElement)); 
     } 
    }); 
} 

調用如下:下面的jquery是做什麼的?

ApplyClickableLinkToClass($j(".rc_blueBtn, .rc_whiteBtn:not(.More)")); 

回答

0
// loop over all selectedElements 
$.each(selectedElements, function() { 
    // find the first (a) that doesn't have the 
    // class ('do-not-apply-clickable-link') 
    // in the currently iterated element 
    var linkElement = $("a:first:not(.do-not-apply-clickable-link)", $(this)); 

    // retrieve the matched (linkElement) href 
    // attribute and store it in (link) 
    var link = linkElement.attr("href"); 

    // bind click event to the currently iterated 
    // element if the (link) was defined... 
    if (!IsNullEmptyOrUndefined(link)) { 
     $(this).click(function(firstLink) { 
      var divToLink = firstLink; 
      return function() { 
       $(divToLink).unbind('click'); 
       if (divToLink.attr("target") != "_blank") { 
        window.location = link; 
        return false; 
       } 
      }; 
     } (linkElement)); 
    } 
}); 
+0

所以添加鏈接到div從裏面第一個HREF發現? – StevieB 2012-03-15 18:32:52

+0

不,它只在鏈接不是'IsNullEmptyOrUndefined'的情況下才會爲每個'selectedElements'添加一個事件... – xandercoded 2012-03-15 18:35:40

+0

而且事件只是在新窗口中打開吧? – StevieB 2012-03-15 18:37:28