亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在腳本中獲取 href id 值,并依賴于 id 值來打開相關頁面

如何在腳本中獲取 href id 值,并依賴于 id 值來打開相關頁面

繁星coding 2022-09-23 14:53:23
我有一個頁面調用主.html,它包括多個網址,這些網址將轉到一個名為central.htm的頁面。中央.htm有一個按鈕,該按鈕將打開目標頁面,具體取決于URL。我讀了這篇文章,采取id值,但我不明白找到id的想法a href我在 main 中應用 id.html并在中央.htm中使用 javascript。但是,我不確定是否可以這樣做,因為我嘗試了以下代碼,但它不起作用。<a href>(更新的代碼) <!DOCTYPE HTML>  <html>  <head>      <title>          this is main.html     </title> </head>  <body><style>//making button look like a tagbutton {background: none!important;border: none;padding: 0!important;color: #069;text-decoration: underline; cursor: pointer;}</style><button href="https://www.mywebsite/central.htm" id="1" onclick="save(this)">First link will go to the central page first and then go to Destination A</button><button href="https://www.mywebsite/central.htm" id="2" onclick="save(this)">Second link will go to the central page first and then go to Destination B</button><button href="https://www.mywebsite/central.htm" id="3" onclick="save(this)">Thrid link will go to the central page first and then go to Destination C</button><button href="https://www.mywebsite/central.htm" id="4" onclick="save(this)">Fourth link will go to the central page first and then go to Destination D</button>                                                    //^added onclick<script>function save(el){  //getting id of href click var ids=el.getAttribute("id"); console.log(ids); localStorage.clear();//clear previous data localStorage.setItem("ids", ids);//add data to storage var href=el.getAttribute("href");//get href console.log(href) window.open(href, '_blank');//open in blank page}</script></body></html>然后所有鏈接都轉到中心頁面,中心頁面確定鏈接然后打開相關目標運行代碼后,如果我單擊“第一個鏈接”按鈕,它將轉到目標 A。但是,如果我單擊其他鏈接按鈕(例如Thrid鏈接),它仍然會轉到目標A。由于我的網站的局限性,我目前無法使用jQuery,所以我專注于使用javascript。請問中央.htm如何從main獲取值.html然后依靠該值打開相關的目標頁面。
查看完整描述

2 回答

?
蠱毒傳說

TA貢獻1895條經驗 獲得超3個贊

您可以在此處使用本地存儲 .i.e: 保存 in 的 id 并使用 和 獲取它到其他頁面。a hreflocalStoragegetItemsetItem


您將如下所示main.html


<style>

//making button look like a tag

  button {

  background: none!important;

  border: none;

  padding: 0!important;

  color: #069;

  text-decoration: underline;

  cursor: pointer;

   }

</style>


  <button href="https://www.mywebsite/central.htm" id="1" onclick="save(this)">First link will go to the central page first and then go to Destination A</button>

  <button href="https://www.mywebsite/central.htm" id="2" onclick="save(this)">Second link will go to the central page first and then go to Destination B</button>

  <button href="https://www.mywebsite/central.htm" id="3" onclick="save(this)">Thrid link will go to the central page first and then go to Destination C</button>

  <button href="https://www.mywebsite/central.htm" id="4" onclick="save(this)">Fourth link will go to the central page first and then go to Destination D</button>

                                                        //^added onclick

  <script>

   function save(el){

      //getting id of href click

     var ids=el.getAttribute("id");

     console.log(ids);

     localStorage.clear();//clear previous data

     localStorage.setItem("ids", ids);//add data to storage

     var href=el.getAttribute("href");//get href

     console.log(href)

     window.open(href, '_blank');//open in blank page

    }


  </script>

然后在你的做如下:central page


 function openDestination() {

if (localStorage.getItem("ids") != null) {

  //get that value

    var ids= localStorage.getItem("ids");

    console.log(ids);

  }

switch(ids) {

   case 1:

      window.open("https://www.mywebsite/DestinationA.html");

      break;

   case 2:

      window.open("https://www.mywebsite/DestinationB.html");

      break;

   case 3:

     window.open("https://www.mywebsite/DestinationC.html");

      break;

   case 4:

      window.open("https://www.mywebsite/DestinationD.html");

      break;

 //if none of those, close the window

   default:

      window.close();

  }

}


查看完整回答
反對 回復 2022-09-23
?
米脂

TA貢獻1836條經驗 獲得超3個贊

您可以使用網址查詢。這意味著您可以在頁面之間傳遞小信息。示例:https://www.mywebsite/central.htm?id=1 稍后在中心頁面上,您可以使用JS獲取此ID,如下所示:


const url = new URLSearchParams(window.location.search);

const id= url.get('id')

就個人而言,我認為這種方式更容易,更具可讀性。


查看完整回答
反對 回復 2022-09-23
  • 2 回答
  • 0 關注
  • 163 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號