javascript是否正确链接到html?为什么脚本没有运行? [英] Is javascript properly linked to html? Why is the script not running?

查看:95
本文介绍了javascript是否正确链接到html?为什么脚本没有运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试学习JavaScript,并找到了有关制作移动响应式标头的教程.除了javasript之外,我有其他所有可用的方法.我尝试链接文件,但是没有用.我发现我需要jQuery来运行脚本,因此我将其链接了.最后,我尝试将实际脚本包含在HTML代码中.仍然无法正常工作

I've been trying to learn JavaScript and found a tutorial about making a mobile responsive header. I have every thing working except the javasript. I have tried linking the file, didn't work. I found out I needed jQuery to run scripts, so I linked that. And lastly I tried to include the actual script in the HTML code. Still not working

这是该教程的链接,是我想要的结果. http://jsfiddle.net/giobongio/DwrGK/2/

Here is the link to the tutorial, my desired result. http://jsfiddle.net/giobongio/DwrGK/2/

这是下面的代码,与示例代码相同.请告诉我我在做错什么.

And here is my code is below, which is identical to the sample code. Please show me what I'm doing wrong.

HTML

<html>
<head>
<script  language="javascript" type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script language="javascript" type="text/javascript">
// create smartbutton
$('nav').before('<div id="smartbutton"></div>');
$('#smartbutton').append('<div class="buttonline"></div>');
$('#smartbutton').append('<div class="buttonline"></div>');
$('#smartbutton').append('<div class="buttonline"></div>');

// add click listener
$('#smartbutton').click(function(event) 
{
  $('nav').animate({height:'toggle'},200);
});

</script>
<link rel="stylesheet" type="text/css" href="C:\Users\Anna\Desktop\Mobile Responsive\header.css">
</head>
<body>
<header>
    <a id="logo" href="#"></a>
    <nav>
        <a href="#" class="navitem">Option 1</a>
        <a href="#" class="navitem">Option 2</a>
        <a href="#" class="navitem">Option 3</a>
    </nav>
</header>
</body>
</html>

JavaScript

// create smartbutton
$('nav').before('<div id="smartbutton"></div>');
$('#smartbutton').append('<div class="buttonline"></div>');
$('#smartbutton').append('<div class="buttonline"></div>');
$('#smartbutton').append('<div class="buttonline"></div>');

// add click listener
$('#smartbutton').click(function(event) 
{
  $('nav').animate({height:'toggle'},200);
});

感谢所有提供帮助的人,我非常感激.我对JavaScript非常陌生,需要一些指导.

Thank you to all who helps, I've very appreciative. I am VERY NEW to JavaScript and I need some guidance.

推荐答案

区别在于示例中的javascript在窗口加载完成时运行.看到j​​sfiddle左侧下拉列表中的onload吗?在创建任何dom元素之前,您的javascript便会执行.为了使此功能快速生效,您可以将javascript移至文档底部,也可以包含jquery并将代码包装在以下脚本标签中:

The difference is that the javascript in the example is run when the window has finished loading. See the onload in the drop down on the left hand side of jsfiddle? Your javascript executes before you've created any dom elements. To get this working quickly you could move the javascript to the bottom of your document,or you could include jquery and wrap your code within the script tags in:

$( document ).ready(function() {
  // your code goes here
});

此方法会将您的代码捕获为一个函数(委托),并在触发时执行它.在这种情况下,当文档准备好时;创建所有html(dom)元素后,请使用英文或英文.这意味着您的JavaScript实际上将具有要处理的元素.

This method will capture your code as a function (delegate) and execute it when it is triggered. In this case, when the document is ready; or in English, when all of the html (dom) elements have been created. This means your javascript will actually have elements to work on.

这篇关于javascript是否正确链接到html?为什么脚本没有运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆