Does jQuery work in a JS script, but not on my site?

Well, something is REALLY frustrating and strange. I have jQuery installed on my server, and I know that it is imported correctly, because when I run simple ...

$(function() { alert('hello') });

He warns "hello." However, when I try to use the css selector ...

$(".image").css("border","3px solid red");

This does not work! Yes, I am 100% sure that there is something with this class name in the file. Here's a real kicker, when I COPIED my code to jsFiddle , it worked fine. What gives?!

+3
source share
3 answers

Your jsFiddle is set to onload in the upper left corner of the jsFiddle window. If you set it to "No Wrap-in Head", which mimics the code in the <head> , then your jsFiddle no longer works.

The onload parameter means that jsFiddle does not start your javascript until the page is loaded.

On your real page, you probably run javascript too soon before the page loads.

You can fix this by putting your javascript in your own .ready() function:

 $(document).ready(function(){ $(".image").css("border","3px solid red"); }); 

Or you can make sure javascript is not loaded / running until the </body> is simple, to make sure that the contents of your page are loaded before the script runs.

 <body> Your HTML content here <script> // your script here that runs after all of the DOM is parsed $(".image").css("border","3px solid red"); </script> </body> 

See this answer for the <script> tag for more details.

+7
source

You check the addition inside $(document).ready(function(){}); ?

 <script src="http://code.jquery.com/jquery-1.10.0.min.js"></script> <script> $(document).ready(function(){ $(".image").css("border","3px solid red"); }); </script> 
0
source
 if u have a internet connection following link useful for you <script src="http://code.jquery.com/jquery-1.10.1.min.js"></script> <script src="http://code.jquery.com/jquery-migrate-1.2.1.min.js"></script> above link put inside a body or before write a script and please verify jquery js file. 
-2
source

Source: https://habr.com/ru/post/1338912/


All Articles