jQuery code removeAttr issue
<input id="mnc" type="text"/>
<input type="text" id="selected" />
$('#mnc').val().length ? $('#selected').attr({
'size': $('#mnc').val()
}) : $('#selected').removeAttr('size');
This gives an error in Firefox 4.
Index or size is negative or greater than the allowed amount" code: "1
Other browsers are able to handle removeAttr
even if attribute does not exist. What i'm doing is checking if input #mnc is empty then remove attribute size from #selected
whether it exists or not.
Check http://jsfiddle.开发者_如何学JAVAnet/zFCtU/1/
It's a bug in Firefox, see the jQuery bug report. A workaround should be present for jQuery Version >= 1.6
.
Edit: Sadly the fix is to be released with 1.6 (not 1.5.2 as I wrote earlier). Firefox 4.0.1 should fix it on the firefox-side though. You either have to decide to expect that 4.0.1 is installed of have to apply the patch yourself.
here is a snippet to look at, it's working
$('#mnc').change(function () {
if ($(this).val().length > 0) {
$('#selected').attr({'size': $(this).val().length});
} else {
$('#selected').removeAttr('size');
}
});
Why don't you try setting it first then unsetting it, to make sure it always exists?
$('#selected').attr('size','').removeAttr('size');
精彩评论