jQuery .select 可以工作,但 Javascript .select 不能

jQuery .select works, but Javascript .select doesnt

本文关键字:select Javascript 不能 工作 jQuery      更新时间:2023-09-26

我正在为摩托罗拉/IE 6设备构建一个移动网站。

由于某种我无法想出的原因,jQuery的.select()函数可以工作,但直接在javascript中调用它却不是。

<input type="text" id="lid" value="" class="as_done">

jQuery的工作方式是这样的:

$('#lid').select();

不起作用的方法是:

document.getElementById('lid').select();

这让我感到困惑。关于为什么会这样的任何想法?

编辑:我没有试图在jQuery中做任何事情。我只是想选择输入框中的文本。我不应该需要jQuery来做到这一点,但是标准方法不起作用。http://www.w3schools.com/jsref/met_text_select.asp

有趣的变化使它对我有用。也许这是Windows Mobile IE 6中的一个错误?

请考虑以下 html:

<input type="hidden" id="lid_as" name="lid" value="1">
<input type="text" id="lid" value="" class="as_done">

调用alert("document.getElementById('lid').name");导致消息lid。这让我觉得它正在抓取ID实际上是lid_as的第一个输入框。

当我将lid_as输入移动到lid框下方时,select功能工作正常。

所以这个HTML使它工作:

<input type="text" id="lid" value="" class="as_done">
<input type="hidden" id="lid_as" name="lid" value="1">

同样,这个问题与WINDOWS MOBILE IE 6有关。

$() 函数返回一个 jquery 对象,而 document.getElementById 返回一个简单的 DOM 对象。

也许是因为

您没有提前调用 .focus() 而发生这种情况

document.getElementById('lid').focus();
document.getElementById('lid').select();

这被包装在jQuerys .select()中