本文介绍了在IE-8和FF-3.5.8中设置readonly属性的jQuery不一致的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我在document.ready中的代码:

  var $ inputs = mySelectors(); 
$ inputs.each(function(){
$(this).attr(readonly,true);

});

此代码适用于IE8,但不适用于FF3.5

IE输出(如IE Developer工具栏所示)< input type =textreadOnly =readonly.... />



FF输出(用Firebug看)
< input type =textreadonly =>

什么是正确的方法来设置它?

  $ elem.attr( 只读, 真); 

  $ elem.attr( 只读, 只读); 

  $ elem.attr( readOnly的, 只读); //注意只有
中的大写字母O

看起来像有一个旧的错误,但不知道它是否得到解决。


参考:

是否有解决跨浏览器不一致问题的方法?如果它是我的代码,我可能会设置它为纯布尔真:

解决方案


  $ elem.attr('readOnly',true); 

当你说这不起作用;究竟发生了什么?



下面是一个示例脚本:



这个使用了我上面的方法,对我来说在Firefox中工作得很好。 Firebug显示这样的属性,因为它的感觉。


This is my code inside document.ready:

var $inputs = mySelectors();
$inputs.each(function() {
$(this).attr("readonly", "true");

});

This code works for IE8, but not for FF3.5

IE output(as seen with IE Developer toolbar)<input type="text" readOnly="readonly"..../>

FF output (seen with Firebug)<input type="text" readonly="">

What is the right way to set it ?

$elem.attr("readonly","true");

or

$elem.attr("readonly","readonly");

or

$elem.attr("readOnly","readonly"); //note the uppercase O in Only

Looks like there was an old bug but not sure if it got resolved.http://osdir.com/ml/jquery-dev/2009-05/msg00115.html

reference:http://api.jquery.com/attr/

IS there a way to tackle this cross browser inconsistency ?

解决方案

If it were my code, I'd probably set it to plain boolean "true":

$elem.attr('readOnly', true);

When you say that it doesn't work; what exactly happens?

Here's a sample script: http://gutfullofbeer.net/readonly.html

That one uses my method above, and it works just fine in Firefox for me. Firebug shows the attribute like that because it feels like it.

这篇关于在IE-8和FF-3.5.8中设置readonly属性的jQuery不一致的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 15:15