【问题标题】:How do you link to a different part of your website in Javascript您如何在 Javascript 中链接到您网站的不同部分
【发布时间】:2022-06-21 21:42:03
【问题描述】:

我创建了一个引导按钮。单击按钮时,我希望它链接到另一个页面。我创建了一个名为 bitcoin.html 的页面。如何将主页的内容链接到我的 bitcoin.html 页面。锚标签似乎在引导按钮内创建了一个超链接——我不希望这样。我创建了一个 onclick 函数并尝试重定向它。但它不起作用。

function relocate {

  location.href = "CRYPTOAPP/bitcoin.html"
}
<!DOCTYPE html>
<html lang="en" dir="ltr">

<head>
  <meta charset="utf-8">
  <title>Cryptocurrency App</title>

  <link rel="preconnect" href="https://fonts.googleapis.com">
  <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
  <link href="https://fonts.googleapis.com/css2?family=Montserrat:wght@400;900&family=Ubuntu:wght@300;400&display=swap" rel="stylesheet">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.3.1/dist/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
  <link rel="stylesheet" href="style.css">

</head>

<body>

  <h1>Cryptos To Watch 2022</h1>
  <div class="container" id="crypocontainer">

      <div class="card bitcoin" id="bitcoin">
        <h1> Bitcoin </h1>
        <img src="images/bitcoin.png" alt="bitcoin image">
        <h2>Current Price </h2>
        <h3 class="btc"> Btc</h3>
        <button type="button" class="btn btn-success" onclick="relocate_bitcoin">Explore</button>
      </div>
</body>

【问题讨论】:

  • 你为什么不使用锚链接?这就是他们的目的。按钮不用于导航。

标签: javascript html jquery css twitter-bootstrap


【解决方案1】:

您的函数名为relocate,但您试图在onclick 事件中调用relocate_bitcoin。您还需要实际调用该函数,即relocate()

也就是说,你不需要一个函数——你可以直接把它放在 onclick 中:

onclick="location.href='CRYPTOAPP/bitcoin.html';"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-23
    • 2012-04-21
    • 1970-01-01
    • 2011-05-02
    • 1970-01-01
    相关资源
    最近更新 更多