如何使用innerHTML在我的代码对齐在中心的文本?



我试图使一个网站使用html, css和js显示笑话时刷新。我可以使用document.write()显示我的笑话,但我不知道如何居中对齐,这样看起来会很好。我搜索了网页,发现我们可以使用inner.HTML函数,但我不明白如何在我的代码中使用它。请帮助。

我的代码,——

Js代码

let jokes = [`I just got my doctor's test results and I'm really upset about it. Turns out, I'm not gonna be a doctor.`,
`My grief counselor died. He was so good, I don’t even care.`,

` Today, I asked my phone “Siri, why am I still single?” and it activated the front camera.`,
` A man wakes from a coma. His wife changes out of her black clothes and, irritated, remarks, “I really cannot depend on you in anything, can I!”`,
` As I get older, I remember all the people I lost along the way. Maybe my budding career as a tour guide was not the right choice.`,
` I was digging in our garden and found a chest full of gold coins. I wanted to run straight home to tell my wife about it. Then I remembered why I was digging in our garden.`,
` The doctor gave me some cream for my skin rash. He said I was a sight for psoriasis.`,
` Don’t challenge Death to a pillow fight. Unless you’re prepared for the reaper cushions.`,
`  I don’t have a carbon footprint. I just drive everywhere.`,
` Even people who are good for nothing have the capacity to bring a smile to your face, like when you push them down the stairs.`,
` A man walks into an enchanted forest and tries to cut down a talking tree. "You can't cut me down," the tree exclaims, "I'm a talking tree!" The man responds, "You may be a talking tree, but you will dialogue."`,
` My mom died when we couldn’t remember her blood type. As she died, she kept telling us to “be positive,” but it’s hard without her.`,
` What does my dad have in common with Nemo? They both can’t be found.`,
` I visited my new friend in his apartment. He told me to make myself at home. So I threw him out. I hate having visitors.`,
` When my Uncle Frank died, he wanted his cremations to be buried in his favorite beer mug. His last wish was to be Frank in Stein.`,
` Do you know the phrase “One man’s trash is another man’s treasure”? Wonderful saying, horrible way to find out that you were adopted.`,
` My husband left a note on the fridge that said, "This isn't working." I'm not sure what he's talking about. I opened the fridge door and it's working fine!`,
` Why did the man miss the funeral? He wasn’t a mourning person.`,
` It’s important to establish a good vocabulary. If I had known the difference between the words “antidote” and “anecdote,” one of my best friends would still be alive.`,
` Want to know how you make any salad into a caesar salad? Stab it twenty-three times.`,
` When I see the names of lovers engraved on a tree, I don't find it cute or romantic. I find it weird how many people take knives with them on outings.`,
` Give a man a match, and he’ll be warm for a few hours. Set him on fire, and he will be warm for the rest of his life.`,
` My wife is mad that I have no sense of direction. So I packed up my stuff and right.`,
` When does a joke become a dad joke? When it leaves you and never comes back.`,
` A priest asks the convicted murderer at the electric chair, “Do you have any last requests?” “Yes,” replies the murderer. “Can you please hold my hand?”`,
` I just read that someone in New York gets stabbed every 52 seconds. Poor guy.`,
` The doctor gave me one year to live, so I shot him with my gun. The judge gave me 15 years. Problem solved.`,
` You know you’re not liked when you get handed the camera every time they take a group photo.`,
` Where did Joe go after getting lost on a minefield? Everywhere.`,
` What's red and bad for your teeth? A brick.`,
` My grandfather said my generation relies too much on the latest technology. So I unplugged his life support.`,
` My parents raised me as an only child, which really pissed off my sister.`,
` What did the Titanic say as it sank? I’m nominating all passengers for the Ice Bucket Challenge!`,
` Why did Mozart kill all of his chickens? When he asked them who the best composer was, they all replied, "Bach, Bach, Bach."`,
` How many emo kids does it take to screw in a lightbulb? None, they all sit in the dark and cry.`,
` I have a stepladder because my real ladder left when I was `,
` They laughed at my crayon drawing. I laughed at their chalk outline.`,
` My husband and I have reached the difficult decision that we do not want children. If anybody does, please just send me your contact details and we can drop them off tomorrow.`,
` I have many jokes about unemployed people, sadly none of them work.`,
` The most corrupt CEOs are the ones who run pretzel companies. They’re always so twisted.`,
` To teach kids about democracy, I let them vote on dinner. They picked tacos. Then I made pizza because they don’t live in a swing state.`,
` I was reading a great book about an immortal cat the other day. It was impossible to put down.`,
` You’re not completely useless. You can always be used as a bad example.`,
` I threw a boomerang a few years ago. I now live in constant fear.`,
`What’s the difference between a hipster and a football player? A football player showers.`,
` I made a website for orphans. It doesn’t have a home page.`,
` The other day, my girlfriend asked me to pass her lipstick but I accidentally passed her a glue stick. She still isn't talking to me.`,
` Why can’t Michael Jackson go within 500 meters of a school? Because he’s dead.`,
` “I’m sorry” and “I apologize” mean the same thing. Except at a funeral.`,
` It turns out a major new study recently found that humans eat more bananas than monkeys. But, I can’t remember the last time I ate a monkey.`,
` Never break someone's heart, they only have one of those. Break their bones instead, they have 206 of them.`,
` What’s the difference between a Lamborghini and a dead body? I don’t have a Lamborghini in my garage.`,
` When ordering food at a new restaurant, my wife asked the waiter what they do to prepare their chicken.“Nothing special,” he explained. “We just tell them they’re going to die.”`,
` What is the difference between Iron man and Iron Woman? One is a superhero and the other is a simple command.`,
` I'll never forget my Grandfather’s last words to me just before he died. "Are you still holding the ladder?"`,
` Sorry, what’s the quickest way to get to the hospital? Easy, just stand in the middle of a busy road.`,
` Why don’t cannibals eat clowns? Because they taste funny.`,
` What’s the hardest part of a vegetable to eat? The wheelchair.`,
` What's the difference between jelly and jam? You can't jelly a clown into the tiny car.`,
` My grandma has the heart of a lion and a lifetime ban from the zoo.`,
` What do you call inexpensive circumcision? A rip-off.`,
` What did the woman with no hands get for Christmas? No idea. She hasn’t opened her present yet.`,
` "I work with animals," the guy says to his Tinder date.  "That's so sweet," she replies. "I like a man who loves animals. Where do you work?" "I'm a butcher," he says.`,
` What’s the difference between a baby and a sweet potato? About 140 calories.`,
` You don’t need a parachute to go skydiving. You need a parachute to go skydiving twice.`,
` Today was the worst day of my life. My ex got hit by a school bus, and I lost my job as a bus driver.`,
` Why was the leper hockey game canceled? It was because of a face-off in the corner.`,
` They say the surest way to a man’s heart is through the stomach. But, I find going through the ribcage a lot easier.`,
` What's the last thing to go through a fly's head as it hits the windshield of a car going 80 mph? Its butt.`,
` What’s the special dish in a restaurant for cannibals? Heads, shoulders, knees, and toes`,
` Why are friends a lot like snow? If you pee on them, they disappear.`,
` What do you call a dog with no legs? Doesn’t matter what you call him, he won’t come anyway.`,
` An apple a day keeps the doctor away. Or at least it does if you throw it hard enough.`,
` Why is it that if you donate a kidney, people love you. But if you donate five kidneys, they call the police.`,
` My senior relatives liked to tease me at weddings, saying things like, “You’ll be next!” They stopped once I started doing the same to them at funerals.`,
` Happy 60th birthday. At last, you can live undisturbed by life insurance agents!`,
` Imagine if you walked into a bar and there was a long line of people waiting to hit you. That's the punch line.`,
`Dark humor is like food. Not everyone gets it.`,
`The cemetery is so crowded. People are just dying to get in.`,
`I was drinking a martini and the waitress screamed “does anyone know CPR?” I yelled, “I know the entire alphabet” and we all laughed and laughed. Well, except one person.`,
`I know a fish that can breakdance! Only for 20 seconds though, and only once.`,
`I childproofed my house. Somehow they still got in!`,
`Why don’t skeletons ever go trick or treating? Because they have no body to go with.`,
`Cremation. My final hope for a smokin’ hot body!`,
`Today on a drive, I decided to go visit my childhood home. I asked the people living there if I could come inside because I was feeling nostalgic, but they refused and slammed the door in my face. My parents are the worst.`,
`People with Covid have no taste!`,
`My favorite Disney movie is The Hunchback of Notre Dame. I love a hero with a twisted back story.`,
`My girlfriend wanted a marriage just like a fairy tale. Fair enough. I gave her a loaf of bread and left her in the forest.`,
`I heard Sony is coming out with a new video game console to help us get through the pandemic. It's called the Plaguestation`,
`I’ve stopped making jokes about Covid to my brother. They flu over his head.`,
`My boss told me to have a good day. So I went home.`,
`I got a job at a library, but it only lasted 15 minutes. Turns out, books about women’s rights shouldn’t go in the Sci-Fi / Fantasy section.`,
`I started crying when dad was cutting onions. Onions was such a good dog.`,
`My daughter asked me how stars die. "Usually an overdose," I told her.`,
`What do you call it when every one of your friends makes too many dumb Covid jokes? A pundemic.`,
`What did Kermit the Frog say at his puppeteer’s funeral? Not a word.`,
`Why is there air conditioning in hospitals? To keep the vegetables cool and fresh.`,
`The guy who stole my diary just died. My thoughts are with his family.`,
`Since the pandemic started, my husband just stands there sadly looking through the window. I should probably go let him inside.`,
`I was shocked when I found out my toaster was not waterproof.`]
let item = jokes[Math.floor(Math.random() * jokes.length)];

let a = document.getElementById
document.write(item)
——

HTML代码

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Jokes</title>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<h1>
Best jokes collection
</h1>
<div id="first"><i>Wanna hear a joke</i></div>
<div id="second"><a href="https://ex-3.arnavttripathi.repl.co/">Refresh</a> for more</div>

<script src="script.js"></script>
<script src="https://replit.com/public/js/replit-badge.js" theme="blue" defer></script>
</body>
</html>
CSS代码——

h1{
color: red;
text-align: center;
font-size: 60px;
}
#first{font-size: 40px;
font-weight: 20px;
text-align: center;
font-style: italic;
}
#second{
margin: 100px;
text-align: center;
font-size: 20px;
}
#second a{
text-decoration: none;
}

试试这个:https://jsfiddle.net/16t73qhe/

  • 添加<p class="joke"></p>
  • 然后将笑话文本设置为document.querySelector('p.joke').textContent = item
  • 确保CSS中心的笑话p.joke { text-align: center;}

然而,有许多更好的方法来实现这一点,但继续学习!

最新更新