当jQuery代码不在单独的文件中时,最佳做法是什么?这个jQuery代码应该放在哪里?
我有一个脚本引用了jQuery和一个包装.ready中的函数的脚本。
如果jQuery代码(2个脚本标记)在头部或刚刚结束标记之前去了?
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script type="text/javascript"> \t
$(document).ready(function()
{
// Register event listeners.
// The ID Selector. Using the # (hash) symbol references the ID of the element.
// jQuery event method: click
// function() is an anonymous function.
$("#paragraph_01").click(function()
{
hide_paragraph_with_specific_id();
});
$("#paragraph_02").click(function()
{
hide_all_paragraphs();
});
$("#paragraph_03").click(function()
{
hide_paragraph_by_class();
});
});
function hide_paragraph_with_specific_id()
{
$("#paragraph_01").hide();
}
function hide_all_paragraphs()
{
$("p").hide();
}
function hide_paragraph_by_class()
{
$(".paragraph_class").hide();
}
</script>
</head>
<body>
<!-- Paragraph tags. -->
<p id="paragraph_01" class="paragraph_class_01">This is paragraph 1.</p>
<p id="paragraph_02" class="paragraph_class">This is paragraph 2.</p>
<p id="paragraph_03" class="paragraph_class">This is paragraph 3.</p>
</body>
</html>
您的代码有一个'$(文件)。就绪()'所以它会在两种情况下工作。此外,这个问题是重复的:https://stackoverflow.com/questions/10994335/javascript-head-body-or-jquery – Santi
也是一个骗局https://stackoverflow.com/questions/2105327/should-jquery-代码-GO-在集管或尺 – j08691