2010-03-10 75 views
1

我试图迭代对象的集合,并试图为每个对象设置一个属性。jQuery - 如何为'this'设置attr ..?

下面是代码:

$(document).ready(function() 
{ 
    $('#clickButton').click(function() 
    { 
     var hiddenVal = $('#hdnVal').val(); 

     $('*').find('*[tabindex]').each(function(index) 
     { 
      //this.setAttribute('tabindex', hiddenVal + this.getAttribute('tabindex')); 
      $(this).attr('tabindex', 'test'); 
     }); 
    }); 
}); 

我无法设置属性与$(this).attr('', '');但JavaScript的方式正常工作。我如何在jQuery中做到这一点?

+0

@Tim:好工作,对代码prettification :-) – 2010-03-10 12:49:18

回答

2

将字符串设置为tabIndex将不起作用,它必须是一个整数。

$(this).attr('tabindex', 'test'); 
alert($(this).attr('tabindex')); 
//^alerts 0 in IE for me, indicating the default is restored 

尝试一些:

$(this).attr('tabindex', 1); 
alert($(this).attr('tabindex')); 
//^alerts 1 
+0

其实我想获得一个隐藏值hiddenVal设置和使用前缀与当前值的现有。当我检查萤火虫,价值不会改变..是因为价值被认为是文字? – Amit 2010-03-10 12:56:36

+0

@Amit:可能。尝试将结果转换为数字,例如'$(this).attr('tabindex',+(hiddenVal + $(this).attr('tabindex')));' – 2010-03-10 13:19:26

+0

嘿,有那么多的跨浏览器问题。 。我用普通的javascript做了它..无论如何谢谢安迪的建议.. – Amit 2010-03-11 09:54:04

相关问题