2012-01-11 58 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')); 
});