javascript - Using jquery in js file - Stack Overflow

Okay so all I am trying to do is make the jquery work in my javascript file which is linked to a html f

Okay so all I am trying to do is make the jquery work in my javascript file which is linked to a html file. I created a totally new file for this to test it. The whole contents of the html file are:

<!DOCTYPE html>
<html>
<script src="C:\Users\Me\Desktop\Programming\jquery-1.12.4"             type="text/javascript"></script>
<script src="check.js"></script>
<head> <h1>test</h1>
</head>
<body>
</body>
</html>

And the whole content of the js file is

$("h1").css("color", "red");

But when I open the html file in chrome the change I put in the js file with jquery doesn't show up (e.g the heading is not red). I just want to figure out how to make it so that it does.

EDIT:

Thanks for the help everybody, I was able to make it work :)

Okay so all I am trying to do is make the jquery work in my javascript file which is linked to a html file. I created a totally new file for this to test it. The whole contents of the html file are:

<!DOCTYPE html>
<html>
<script src="C:\Users\Me\Desktop\Programming\jquery-1.12.4"             type="text/javascript"></script>
<script src="check.js"></script>
<head> <h1>test</h1>
</head>
<body>
</body>
</html>

And the whole content of the js file is

$("h1").css("color", "red");

But when I open the html file in chrome the change I put in the js file with jquery doesn't show up (e.g the heading is not red). I just want to figure out how to make it so that it does.

EDIT:

Thanks for the help everybody, I was able to make it work :)

Share Improve this question edited May 26, 2016 at 22:54 user1778543 asked May 26, 2016 at 20:49 user1778543user1778543 1672 silver badges9 bronze badges 1
  • 8 You put H1 inside the HEAD? Wierd – Alon Eitan Commented May 26, 2016 at 20:51
Add a ment  | 

6 Answers 6

Reset to default 5

did you try this?

$(document).ready(function(){
    $("h1").css("color", "red");
});

not sure but, does your browser access local files directly? im pretty sure that browser sandbox gonna reject that, maybe cors plugin on chrome?

EDIT:

try importing this instead...

<script src="https://ajax.googleapis./ajax/libs/jquery/2.2.2/jquery.min.js"></script>

Try to use jQuery CDN, this way you don't need to worry if jQuery loaded correctly or not.

$(document).ready(function() {
  $("h1").css("color", "red");
});
<!DOCTYPE html>
<html>
<script src="https://code.jquery./jquery-1.12.4.min.js"></script>
<script src="check.js"></script>
<head></head>
<body>
  <h1>test</h1>
</body>
</html>

  1. Move your <h1> tag to be in the <body>
  2. Move your <script> tags to the <head>
  3. Replace your code in your JS file with what imvain2 said, so that the JS will load once your document is ready.
  4. Make sure you are correctly referencing your jQuery script. You can even use this to test it out:

    <script src="https://ajax.googleapis./ajax/libs/jquery/2.2.2/jquery.min.js"></script>
    

There are a couple issue as described in other posts. All your meta, script, css references, etc. should go int the <head> and never header tags. Also need to correctly reference your javascript files.

<head> 
</head>
<body>
<header>
  <h1>test</h1>
</header>
</body>

Example: JSFiddle

If you want to use jquery in .js file all you need to do is type: src=" ..." above code.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745115993a4612129.html

相关推荐

  • javascript - Using jquery in js file - Stack Overflow

    Okay so all I am trying to do is make the jquery work in my javascript file which is linked to a html f

    10小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信