Internet Explorer 8 中的 JQuery 问题

发布于 2024-12-04 04:59:49 字数 1312 浏览 0 评论 0 原文

以下代码在 Firefox 中运行良好,但在 IE 中则不行。我已经尝试对标头和脚本调用字符串进行各种调整,但是没有任何方法可以使 IE 运行脚本。此外,IE 中的所有安全功能均已关闭。

下面的代码有什么问题吗?

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Smartube</title>
    <link href="css/default.css" rel="stylesheet" type="text/css" />
    <script src="jquery.js" type="text/javascript"></script>
    <script src="tubeutil.js" type="text/javascript"></script>
    <script src="init.js" type="text/javascript"></script>
    <script type="text/javascript">
    function formfocus() {
    document.getElementById('1').focus();
    }
    window.onload = formfocus;
    </script>
    </head>

    <body>



<div class="wrapper">
    <img class="displayed" src="gfx/sb.png">
    <form class="blocks" action="" method="get">
        <p><input type="text"  class="search" id="1" /></p>
        <p><input type="Submit" class="btn" value="" /></p>
        <ul class="reset autocomplete"></ul>
    </form>

    <ul class="reset videos"></ul>

</div>
    </body>
    </html>

The following code works fine in Firefox, but not in IE. I have tried various tweaks to both header and script call string, however nothing will make IE run the scripts. Also all security is turned off in IE.

Is there anything wrong in the following code?

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Smartube</title>
    <link href="css/default.css" rel="stylesheet" type="text/css" />
    <script src="jquery.js" type="text/javascript"></script>
    <script src="tubeutil.js" type="text/javascript"></script>
    <script src="init.js" type="text/javascript"></script>
    <script type="text/javascript">
    function formfocus() {
    document.getElementById('1').focus();
    }
    window.onload = formfocus;
    </script>
    </head>

    <body>



<div class="wrapper">
    <img class="displayed" src="gfx/sb.png">
    <form class="blocks" action="" method="get">
        <p><input type="text"  class="search" id="1" /></p>
        <p><input type="Submit" class="btn" value="" /></p>
        <ul class="reset autocomplete"></ul>
    </form>

    <ul class="reset videos"></ul>

</div>
    </body>
    </html>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

轻许诺言 2024-12-11 04:59:50

下面的代码有什么问题吗?

有件事让我觉得错误,但我无法知道这是否是您问题的原因(我看不到您的 JavaScript 的其余部分)。

您缺少有效的文档类型,因此 Internet Explorer 处于怪异模式,这非常糟糕!

将其添加为第一行:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

Is there anything wrong in the following code?

One thing strikes me as wrong, but there's no way I can know if it's the cause of your problems (I can't see the rest of your JavaScript).

You're missing a valid doctype, so Internet Explorer is in Quirks Mode, which is very bad!

Add this as the very first line:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
醉殇 2024-12-11 04:59:50

正如您可以阅读关于这个问题

ID 和 NAME 令牌必须以字母 ([A-Za-z]) 开头,并且可以是
后跟任意数量的字母、数字 ([0-9])、连字符 (“-”)、
下划线(“_”)、冒号(“:”)和句点(“.”)。

所以“1”不是一个有效的ID。将该 id 更改为“id1”或“searchfield”以使脚本在所有浏览器中运行。

作为旁注:正如其他人所说,您实际上并没有在这里使用 jqery,但这不是问题所在。

as you can read on this question

ID and NAME tokens must begin with a letter ([A-Za-z]) and may be
followed by any number of letters, digits ([0-9]), hyphens ("-"),
underscores ("_"), colons (":"), and periods (".").

so "1" is not a valid id. change that id to "id1" or "searchfield" to make the script work in all browsers.

as a sidenote: as others said, you aren't actually using jqery here, but this isn't the problem.

波浪屿的海角声 2024-12-11 04:59:50

第一: id="1" 不是有效的 ID

第二:既然你使用的是 jQuery,我建议你使用 jQuery (!):

$(function(){
  $('#that-form-element').focus();
});

一旦页面运行,就会运行 $('#that-form-element').focus(); (DOM) 已准备就绪。

First: id="1" is not a valid ID.

Second: Since you're using jQuery, I'd suggest that you use jQuery (!):

$(function(){
  $('#that-form-element').focus();
});

That will run $('#that-form-element').focus(); once the page (DOM) is ready.

耳钉梦 2024-12-11 04:59:50

id 指定 HTML 文档中元素的唯一 ID。

命名规则:

Must begin with a letter A-Z or a-z
Can be followed by: letters (A-Za-z), digits (0-9), hyphens ("-"), underscores ("_"), colons (":"), and periods (".")
Values are case-sensitive

id Specifies a unique id for an element within an HTML Document.

Naming rules:

Must begin with a letter A-Z or a-z
Can be followed by: letters (A-Za-z), digits (0-9), hyphens ("-"), underscores ("_"), colons (":"), and periods (".")
Values are case-sensitive
我喜欢麦丽素 2024-12-11 04:59:50

你根本没有使用jquery。

使用这个符号:

$(function() {
    $('#1').focus();
});

而不是所有的 javascript

You are not using jquery at all.

use this notation :

$(function() {
    $('#1').focus();
});

instead of all your javascript

不喜欢何必死缠烂打 2024-12-11 04:59:49

您的代码并不是真正的 jQuery,而是直接的 javascript,但您的问题是 W3C 标准要求 ID 以字母开头(请参阅 http://www.w3schools.com/tags/att_standard_id.asp)。

您可以将脚本编写为 jQuery,如下所示:

function formfocus( ) {
    $('#myid').focus( );
}
$(window).load( formfocus );

编辑:好的,我刚刚在 IE 中测试了以下内容:

HTML:

<div class="wrapper">
 <img class="displayed" src="gfx/sb.png">
    <form class="blocks" action="" method="get">
        <p><input type="text"  class="search" id="my1" value="" /></p>
        <p><input type="Submit" class="btn" value="" /></p>
        <ul class="reset autocomplete"></ul>
    </form>

    <ul class="reset videos"></ul>

</div>

JS:

$(document).ready( function ( ) {
  $('#my1').focus( );
});

这适用于 IE9,所以我认为它也应该适用于 8。测试版本在这里:http://jsbin.com/ipezey/edit#javascript,html

另外,根据三十点的回答,此测试版本有 HTML 5 文档类型,而您的版本没有。这可能是您问题的根源。

Your code isn't really jQuery, it's straight javascript, but your problem here is that the W3C standard requires an ID to begin with a letter (see http://www.w3schools.com/tags/att_standard_id.asp).

You could write your script as jQuery like so:

function formfocus( ) {
    $('#myid').focus( );
}
$(window).load( formfocus );

EDIT: OK, I've just tested the following in IE:

HTML:

<div class="wrapper">
 <img class="displayed" src="gfx/sb.png">
    <form class="blocks" action="" method="get">
        <p><input type="text"  class="search" id="my1" value="" /></p>
        <p><input type="Submit" class="btn" value="" /></p>
        <ul class="reset autocomplete"></ul>
    </form>

    <ul class="reset videos"></ul>

</div>

JS:

$(document).ready( function ( ) {
  $('#my1').focus( );
});

That works in IE9, so I think it should work in 8 too. Test version is here: http://jsbin.com/ipezey/edit#javascript,html

Also, as per thirtydot's answer, This test version has an HTML 5 doctype, whereas your version has none. This could be the root of your problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文