2012-01-11 78 views
1

第一次發佈在這裏,通過幾個可能類似的標題看,仍然沒有.. :(jquery attr()的值沒有被替換?

試圖與jQuery/JavaScript的最新日期。嗯,我的問題是,我想改變我的如果文檔寬度低於1022像素,錨點元素的href屬性值已經能夠成功警告href值,但是我的註釋ALERT(哪個工作)href字符串沒有更新......不知道爲什麼,任何幫助,將不勝感激。

if(document.width < 1022) 
{ 
    var allLinks = $('.filtered_portfolio li a'); 
    allLinks.each(function(){ 
     var thisLink = $(this); 
     anchorLinkUrl = this 
     alert (anchorLinkUrl); 

     if(anchorLinkUrl.indexOf('[iframe]=true&lightbox[width]=800&lightbox[height]=540') != -1){ 
      anchorLinkUrl.replace('[iframe]=true&lightbox[width]=800&lightbox[height]=540','donkey'); 
      //alert('hi'); 
     } 
    }); 
}  

回答

1

anchorLinkUrl變量指定的DOM元素,而不僅僅是href值。要更新鏈接的href,試試這個(U ntested):

if(document.width < 1022) 
     { 
      $('.filtered_portfolio li a').each(function(){ 
       this.href = this.href.replace('[iframe]=true&lightbox[width]=800&lightbox[height]=540','donkey'); 
       }); 
     } 
0

Here是你如何使用jQuery的.attr()函數來獲取/設置所選元素的屬性的例子,希望這有助於。

$('a').toggle(function(e) { 
    e.preventDefault(); 
    // setting an attribute 
    $(this).attr('href', 'www.yahoo.com'); 
},function(e) { 
    e.preventDefault(); 
    // setting an attribute 
    $(this).attr('href', 'www.google.com'); 
}).click(function(e) { 
    e.preventDefault(); 
    // getting an attribute 
    $('#foo').text($(this).attr('href')); 
});