[英]JQuery problem in Internet Explorer 8
以下代碼在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>
你的代碼不是真正的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
另外,根據thirtydot的回答,此測試版本具有HTML 5 doctype,而您的版本沒有。 這可能是您問題的根源。
以下代碼有什么問題嗎?
有一件事讓我覺得錯誤,但是我無法知道這是否是你問題的原因(我看不到你的其他JavaScript)。
您錯過了有效的doctype,因此Internet Explorer處於Quirks模式 ,非常糟糕!
將此添加為第一行:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
你可以閱讀這個問題
ID和NAME令牌必須以字母([A-Za-z])開頭,后面可以跟任意數量的字母,數字([0-9]),連字符(“ - ”),下划線(“_”) ,冒號(“:”)和句號(“。”)。
所以“1”不是有效的id。 將該ID更改為“id1”或“searchfield”,以使腳本在所有瀏覽器中都有效。
作為旁注:正如其他人所說,你實際上並沒有在這里使用jqery,但這不是問題所在。
首先: id="1"
不是有效的ID 。
第二:既然你正在使用jQuery,我建議你使用jQuery(!):
$(function(){
$('#that-form-element').focus();
});
那將運行$('#that-form-element').focus();
一旦頁面(DOM)准備好了。
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
你根本沒有使用jquery。
使用這種表示法:
$(function() {
$('#1').focus();
});
而不是你所有的JavaScript
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.