jQuery - how to set attr for 'this'..?
I was trying to iterate through a collection of objects and was trying to set a开发者_StackOverflow社区n attribute for each of the object.
Here is the code:
$(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');
});
});
});
I could not set the attribute with $(this).attr('', '');
but the JavaScript way works fine. How can I do it in jQuery?
Setting a string to tabIndex
will not work, it must be an integer.
$(this).attr('tabindex', 'test');
alert($(this).attr('tabindex'));
// ^ alerts 0 in IE for me, indicating the default is restored
Try a number:
$(this).attr('tabindex', 1);
alert($(this).attr('tabindex'));
// ^ alerts 1
精彩评论