如何在 JavaScript 中移除連結(錨文字)的第一個字元?
此處,“Aabout_us”和“Hhome_page”的拼寫有誤,作為錨文字。
您可以使用 substring(1) 和 innerHTML 來移除第一個字元並將它們分別正確顯示為“about_us”和“home_page”。
示例
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initialscale=1.0">
<title>Document</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/fontawesome/
4.7.0/css/font-awesome.min.css">
</head>
<body>
<div class="linkDemo">
<div>
<a href="">Aabout_us</a>
</div>
</div>
<div class="linkDemo">
<div>
<a href="">Hhome_page</a>
</div>
</div>
<script>
[...document.querySelectorAll('.linkDemo div a')].forEach(obj=>
obj.innerHTML=obj.innerHTML.substring(1))
</script>
</body>
</html>要執行以上程式,請儲存檔名“anyName.html(index.html)”,然後右擊檔案。在 VS 程式碼編輯器中選擇選項“使用 Live Server 開啟”。
輸出
移除第一個字元後,這會生成以下正確形式的輸出 −

廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP