1

我正在尝试使用外部 jQuery 脚本更改 HTML 文档的 CSS 属性。jquery 应该使用赋予它的类来更改所有段落。

我不允许更改 HTML 文档。

我首先在 HTML 文档中编写了 jquery,它运行良好。现在我需要放入一个外部文件,但我认为我做错了什么。

我不允许链接到在 HTML 文件中调用它的最新版本的 Jquery。如何从我的外部文件中调用它?那可能吗?

请在下面查看我的代码,谢谢!


<html>
    <head>
        <title>comp1911 final exam part 2 #3</title>

        <script type="text/javascript" src="question3.js"></script>

    </head>
<body>

<p class="sports"> 1. At the age of 10, Gretzky scores 378 goals in Brantford's atom league - still an age group record.

Circa 1968 - Wayne dominated the Brantford atom league, scoring 378 goals.

2. While playing in the prestigious International Pee Wee Hockey Tournament in Quebec City's Le Colisee, the 14-year-old Gretzky is frequently besieged by autograph seekers.

3. As a teenager, young Wayne often competes against players much older. At 14 he plays in Tier II junior and major junior at 15.

4. Wayne first dons his now famous No. 99 jersey as a member of the Sault Ste. Marie Greyhounds. Originally wanting to wear No. 9 like his idol Gordie Howe, Gretzky settles on his trademark number after wearing No. 19 for a few weeks

5. In May 1978, Gretzky turns pro at 17 with the Indianapolis Racers of the World Hockey Association.

6. The financially strapped Indianapolis franchise trades Gretzky to the Edmonton Oilers of the WHA in May of 1978.

7. In his first season with the Oilers, Gretzky registers 46 goals and 110 points on his way to capturing WHA rookie of the year honors.</p>
<p class="books">Nikolai Vasilievich Gogol was a Ukrainian-born Russian  novelist, humourist, and dramatist.[2]

He is considered the father of modern Russian realism. His early works, such as Evenings on a Farm Near Dikanka, were heavily influenced by his Ukrainian upbringing and identity.[3][4] His more mature writing satirised the corrupt bureaucracy of the Russian Empire, leading to his exile. On his return, he immersed himself in the Orthodox Church.[5] The novels Taras Bul'ba (1835; 1842 [revised edition]) and Dead Souls (1842), the play The Inspector-General (1836, 1842), and the short stories Diary of a Madman, The Nose and The Overcoat (1842) are among his best known works. With their scrupulous and scathing realism, ethical criticism as well as philosophical depth, they remain some of the most important works of world literature.</p>
<p class="comedy">Jack Handey (born 25 February 1949) is an American  humorist. He is best known for his Deep Thoughts by Jack Handey, a large body of surrealistic  one-liner jokes, as well as his "Fuzzy Memories" and "My Big Thick Novel" shorts. Although many people assume otherwise,[1][2]  Handey is a real person, not a pen name or a character.</p>
<p class="sports books comedy">I found this book: http://www.amazon.com/Teed-Off-Laughs-Greens-Sports/dp/0233995005/ref=sr_1_1?ie=UTF8&s=books&qid=1277749796&sr=1-1 You don't have to be good to play, but a low level of self-esteem is helpful! Humorous collection of colour photographs and hilarious text reduces our heroes of the golf course to mere mortals. No golfing disaster is left secret and the 80 photographs only go to prove the fallibility of international stars.</p>
<p class="life">To my dearest daughter,

Now that you are ten, I want to write to you about something that is important to me. Have you ever wondered how we know the things that we know? How do we know, for instance, that the stars, which look like tiny pinpricks in the sky, are really huge balls of fire like the Sun and very far away? And how do we know that the Earth is a smaller ball whirling round one of those stars, the Sun?
The answer to these questions is ‘evidence’.

Sometimes evidence means actually seeing (or hearing, feeling, smelling….) that something is true. Astronauts have traveled far enough from the Earth to see with their own eyes that it is round. Sometimes our eyes need help. The ‘evening star’ looks like a bright twinkle in the sky but with a telescope you can see that it is a beautiful ball – the planet we call Venus. Something that you learn by direct seeing (or hearing or feeling…) is called an observation.

Often evidence isn’t just observation on its own, but observation always lies at the back of it. If there’s been a murder, often nobody (except the murderer and the dead person!) actually observed it. But detectives can gather together lots of other observations which may all point towards a particular suspect. If a person’s fingerprints match those found on a dagger, this is evidence that he touched it. It doesn’t prove that he did the murder, but it can help when it’s joined up with lots of other evidence. Sometimes a detective can think about a whole lot of observations and suddenly realize that they all fall into place and make sense if so-and-so did the murder.</p>

<p class="books life">SIMPLIFY YOUR LIFE: 100 WAYS TO SLOW DOWN AND ENJOY THE THINGS THAT REALLY MATTER [Paperback]
Elaine St. James
Elaine St. James (Author)
› Visit Amazon's Elaine St. James Page
Find all the books, read about the author, and more.
See search results for this author
Are you an author? Learn about Author Central
(Author) For everyone who is overwhelmed by the increasing demands in their lives, here is the ideal guide for slowing down and finding peace of mind. In separate chapters covering career, household, health, social, finance, and personal affairs, this thought-provoking book offers one hundred proven, practical steps for creating a simple but elegant lifestyle. </p>
<p class="comedy books">Our Front Pages: 21 Years of Greatness, Virtue, and Moral Rectitude from America's Finest News Source (Onion Presents): Since its founding by a bloodthirsty tyrant in 1756, The Onion has not merely changed the way we think about the news -- it has changed whether we think about the news at all. As the first decade of this new millennium draws to a close, Our Front Pages shows us the first thing that presidents, kings, prime ministers, and popes saw when they opened their eyes each morning for the last 21 years. Now you, the common reader and citizen, can see what they saw and be as informed as they were with this important retrospective of the past two decades. You, too, will realize what generations before have realized and generations yet unborn will some day realize in turn: The Onion is not merely the chronicle of America. The Onion is America. </p>
<p class="life">Richard Feynman: Some years ago I had a conversation with a layman about flying saucers — because I am scientific I know all about flying saucers! I said "I don't think there are flying saucers'. So my antagonist said, "Is it impossible that there are flying saucers? Can you prove that it's impossible?" "No", I said, "I can't prove it's impossible. It's just very unlikely". At that he said, "You are very unscientific. If you can't prove it impossible then how can you say that it's unlikely?" But that is the way that is scientific. It is scientific only to say what is more likely and what less likely, and not to be proving all the time the possible and impossible. To define what I mean, I might have said to him, "Listen, I mean that from my knowledge of the world that I see around me, I think that it is much more likely that the reports of flying saucers are the results of the known irrational characteristics of terrestrial intelligence than of the unknown rational efforts of extra-terrestrial intelligence." It is just more likely. That is all.

* The Character of Physical Law. Cornell University Messenger Lectures (1964)
</p>
<p class="sports books life">Tiger Woods' father Earl Woods wrote "Training a Tiger: A Father's Guide to Raising a Winner in Both Golf and Life". The first thing you'll learn in this book, which details how Earl Woods raised his son, Tiger, to become probably the greatest golfer who ever lived, is that your child will never be another Tiger Woods. Tiger is a prodigy in the sense that Mozart was a prodigy. But even if your child has completely ordinary abilities, Training a Tiger shows how to pass on your love for a sport to your child. Most of the book is specific to golf, but the same lessons can be applied to any activity. </p>
<p class="sports">Ice hockey (hockey in countries where it is the most popular form of hockey) is a team sport played on ice, in which skaters use sticks to direct a puck into the opposing team's goal. It is a fast-paced physical sport. Ice hockey is most popular in areas that are sufficiently cold for natural reliable seasonal ice cover, such as Canada, the Czech Republic, Latvia, the Nordic countries (especially Sweden and Finland), the northern United States, Russia, Slovakia, and Switzerland. With the advent of indoor artificial ice rinks it has become a year-round pastime in these areas. Ice hockey is one of the four major North American professional sports. Worldwide the National Hockey League (NHL) is the highest level for men and both the Canadian Women's Hockey League  (CWHL) and the Western Women's Hockey League (WWHL) are the highest levels for women. It is the official national winter sport of Canada, where the game enjoys immense popularity.

While there are 68 total members of the International Ice Hockey Federation (IIHF), 162 of 177 medals at the IIHF World Championships have been taken by seven nations: Canada, the Czech Republic, Finland, Russia, Slovakia, Sweden and the United States.[1][2] Of the 64 medals awarded in men's competition at the Olympic level from 1920 on, only six did not go to the one of those countries. All twelve Olympic and 36 IIHF World Women Championships medals have gone to one of those seven countries, and every gold medal in both competitions has been won by either Canada or the United States</p>

$(".sports").css({"border":"solid"}); $(".sports").css({"borderColor":"green"})

            $(".life").css({"color":"red"})

            $(".books").css({"text-align":"right"})

            $(".comedy").css({"font-size":"200%"})

修订的外部脚本:

$(".sports").css({"border":"solid"});
$(".sports").css({"borderColor":"green"})

$(".life").css({"color":"red"})

$(".books").css({"text-align":"right"})

$(".comedy").css({"font-size":"200%"})
4

2 回答 2

1

而不是从你的外部 JS 文件中实际调用 jQuery,你可以复制粘贴整个东西(当然是缩小的)。1.9.1 的缩小版可以在这里找到。只需确保将其粘贴到外部文件的最顶部,以便所有使用 jQuery 的代码都紧随其后。

于 2013-03-29T02:02:57.307 回答
0

jsFiddle 演示

为什么要使用jquery?您可以结合迭代、内置功能和getElementsByClassName.

js

window.onload = function(){
 var sports = document.getElementsByClassName("sports");
 for( var i = 0; i < sports.length; i++ ){
  sports[i].style.border = "solid";     
  sports[i].style.borderColor = "green";     
 }
 var life = document.getElementsByClassName("life");
 for( var i = 0; i < life.length; i++ ){
  life[i].style.color = "red";     
 }
 var books = document.getElementsByClassName("books");
 for( var i = 0; i < books.length; i++ ){
  books[i].style.textAlign = "right";     
 }

 var comedy = document.getElementsByClassName("comedy");
 for( var i = 0; i < comedy.length; i++ ){
  comedy[i].style.fontSize = "200%";     
 }
};
于 2013-03-29T02:14:30.517 回答