gpt4 book ai didi

javascript - jQuery 不能在 HTML 文件中工作

转载 作者:可可西里 更新时间:2023-11-01 12:59:29 24 4
gpt4 key购买 nike

我尝试了多种导入 jQuery 的变体,但没有任何效果。当我运行它时,正文中的所有内容都会显示出来,但 jQuery 函数不起作用。这是我当前的代码:

<!DOCTYPE html>
<html>
<head>

<script type='text/javascript' src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

<script type='text/javascript'>

$(window).load(function(){
$('select').on('change',function(){
var value=$(this).val();
var output='';
for(var i=1;i<=value;i++)
{
output+='<div>Your Text</div>';
}
$('#test').empty().append(output);
});
});

</script>

</head>

<body>
<select>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="10">10</option>
</select>

<span id="test">
</span>
</body>

</html>

我能做些什么来修复它?

这是我在控制台中遇到的错误:

The resource from “https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.mi%C3%A2%E2%82%AC%C5%92%C3%A2%E2%82%AC%E2%80%B9n.js” was blocked due to MIME type mismatch (X-Content-Type-Options: nosniff). testy.html

ReferenceError: $ is not defined[Learn More]

The character encoding of the HTML document was not declared. The document will render with garbled text in some browser configurations if the document contains characters from outside the US-ASCII range. The character encoding of the page must be declared in the document or in the transfer protocol.

最佳答案

执行以下步骤:-

复制 jquery 库代码 ( https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js ) 代码并在当前工作目录 ( jquery.min.js ) 中以相同的名称保存它:-

现在使用这段代码:-

<!DOCTYPE html>
<html>
<head>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<meta content="utf-8" http-equiv="encoding"><!-- this is for removing character encoding error-->

<script type='text/javascript' src="jquery.min.js"></script><!-- see the change here -->
</head>
<body>
<select>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="10">10</option>
</select>
<span id="test">
</span>
</body>
</html>
<script type = "text/javascript">
$(document).ready(function(){
$('select').on('change',function(){
var value=$(this).val();
var output='';
for(var i=1;i<=value;i++)
{
output+='<div>Your Text</div>';
}
$('#test').html(output); //html will do everything(removing and then adding)
});
});
</script>

注意:- 您可以这样尝试一次:-

<script type='text/javascript' src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

关于javascript - jQuery 不能在 HTML 文件中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42430670/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com