gpt4 book ai didi

javascript - 将 javascript 放在 head 或 body 中有区别吗?

转载 作者:行者123 更新时间:2023-11-28 12:22:55 28 4
gpt4 key购买 nike

我对 javascript 很陌生,无法确定使用“selectize”输入的这两段代码之间是否存在差异。

两者都有效,但我不知道哪个更好或者它们是否真的相同。如果有的话,哪一个更可取?是否有关于在 html 文档中放置 javascript 的“规则”或最佳实践?

如果这是非常微不足道的事情,我们深表歉意:)

tl;博士

第一个代码有 $('#location').selectize({ .... })里面<body>标签

第二个代码具有相同的 $('#location').selectize({ .... })但在 <head>标签并包含在

$(document).ready(function () {
$('#location').selectize({
....
})
)}

<html lang="en">
<head>
<title>title</title>
<link href="https://cdnjs.cloudflare.com/ajax/libs/selectize.js/0.12.1/css/selectize.bootstrap3.css"
rel="stylesheet">
<script src="https://cdnjs.cloudflare.com/ajax/libs/selectize.js/0.12.1/js/standalone/selectize.js"></script>
</head>
<body>
<form role="form" class="form-vertical" method="POST" action="/profile">
<div class="form-group">
<label for="location">location</label>


<input type="text" value="Lorien, 58120 Corancy, France" placeholder="" name="location"
id="location" class="form-control selectized" tabindex="-1" style="display: none;">

<div class="selectize-control form-control single">
<div class="selectize-input items full has-options has-items">
<div class="item" data-value="Lorien, 58120 Corancy, France">Lorien, 58120 Corancy,
France
</div>
<input type="text" autocomplete="off" tabindex="" style="width: 4px;"></div>
<div class="selectize-dropdown single form-control" style="display: none;">
<div class="selectize-dropdown-content"></div>
</div>
</div>
<input type="submit" value="Update" name="submit" id="submit">
</div>
</form>
<script>
$('#location').selectize({
valueField: 'formatted_address',
labelField: 'formatted_address',
searchField: 'formatted_address',
maxItems: 1,
delimiter: ';',
create: false,
load: function (query, callback) {
if (!query.length) return callback();
$.ajax({
url: "googleloc",
type: 'GET',
dataType: 'json',
data: {
search: query,
},
error: function () {
callback();
},
success: function (res) {
callback(res.results);
googresults = res.results;
}
});
},
onChange: function (value) {
if (!value.length) return;
for (var key in googresults) {
if (googresults[key].formatted_address == value) {
var lat = googresults[key].geometry.location.lat;
var lng = googresults[key].geometry.location.lng;
for (var component in googresults[key].address_components) {
if (googresults[key].address_components[component].types[0] == "country") {
var cc = googresults[key].address_components[component].short_name;
}
}
}
}
$('#latitude').val(lat);
$('#longitude').val(lng);
$('#country_code').val(cc);
}
});
</script>
</body>
</html>

版本 2 的头部有选定的项目

<html lang="en">
<head>
<title>title</title>
<link href="https://cdnjs.cloudflare.com/ajax/libs/selectize.js/0.12.1/css/selectize.bootstrap3.css"
rel="stylesheet">
<script src="https://cdnjs.cloudflare.com/ajax/libs/selectize.js/0.12.1/js/standalone/selectize.js"></script>
<script>
$(document).ready(function () {
$('#location').selectize({
valueField: 'formatted_address',
labelField: 'formatted_address',
searchField: 'formatted_address',
maxItems: 1,
delimiter: ';',
create: false,
load: function (query, callback) {
if (!query.length) return callback();
$.ajax({
url: "googleloc",
type: 'GET',
dataType: 'json',
data: {
search: query,
},
error: function () {
callback();
},
success: function (res) {
callback(res.results);
googresults = res.results;
}
});
},
onChange: function (value) {
if (!value.length) return;
for (var key in googresults) {
if (googresults[key].formatted_address == value) {
var lat = googresults[key].geometry.location.lat;
var lng = googresults[key].geometry.location.lng;
for (var component in googresults[key].address_components) {
if (googresults[key].address_components[component].types[0] == "country") {
var cc = googresults[key].address_components[component].short_name;
}
}
}
}
$('#latitude').val(lat);
$('#longitude').val(lng);
$('#country_code').val(cc);
}
});
});
</script>
</head>
<body>
<form role="form" class="form-vertical" method="POST" action="/profile">
<div class="form-group">
<label for="location">location</label>


<input type="text" value="Lorien, 58120 Corancy, France" placeholder="" name="location"
id="location" class="form-control selectized" tabindex="-1" style="display: none;">

<div class="selectize-control form-control single">
<div class="selectize-input items full has-options has-items">
<div class="item" data-value="Lorien, 58120 Corancy, France">Lorien, 58120 Corancy,
France
</div>
<input type="text" autocomplete="off" tabindex="" style="width: 4px;"></div>
<div class="selectize-dropdown single form-control" style="display: none;">
<div class="selectize-dropdown-content"></div>
</div>
</div>
<input type="submit" value="Update" name="submit" id="submit">
</div>
</form>
</body>
</html>

最佳答案

最好将 javascript 放在 </body> 之前的底部。 。这样它就不会阻止渲染(有利于性能)。另外,此时您就知道 DOM(HTML 元素树)已经为您准备好了。

$(document).ready(function () {})是 jQuery 等待 DOM 准备就绪的方式。如果你把你的javascript放在<head>中你必须使用这种技术,因为显然你想要使用的所有实际 HTML 都在它之后并且需要解析。一旦 DOM 准备好,回调(传递给 .ready() 的函数)就会被调用。

关于javascript - 将 javascript 放在 head 或 body 中有区别吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34212089/

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