Skip to content

有害部落格同好會

2006/01/20 / JavaScript, VBScript

getElementByClass

資料來源: webmasterworld、mixfog

Javascript 中 的 getElementById 十分常用,但在標準的頁面中,一個id只能出現一次,如果我想同時控制多個元素,例如點一個鏈接, 讓多個層隱藏,該怎麼做?

用 class,當然,同一個class是可以允許在頁面中重複出現的,那麼有沒有getElementByClass呢?

沒有, 但是可以解決:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<script language="javascript">
 
//Create an array
var allPageTags = new Array();
 
function hideDivWithClasses(theClass) {
 
//Populate the array with all the page tags
var allPageTags=document.getElementsByTagName("div");
 
//Cycle through the tags using a for loop
for (i=0; i <  allPageTags.length;) {
 
//Pick out the tags with our class name
if (allPageTags[i].className==theClass) {
//Manipulate this in whatever way you want
allPageTags[i].style.display='none';
i++;
}
}
}
</script>

<script language="javascript"> //Create an array var allPageTags = new Array(); function hideDivWithClasses(theClass) { //Populate the array with all the page tags var allPageTags=document.getElementsByTagName("div"); //Cycle through the tags using a for loop for (i=0; i <  allPageTags.length;) { //Pick out the tags with our class name if (allPageTags[i].className==theClass) { //Manipulate this in whatever way you want allPageTags[i].style.display='none'; i++; } } } </script>

Post navigation

Previous Post:

CSS固定表頭的HTML表格

Next Post:

Ajax without using the XmlHttpRequest Object

分類

  • 新奇有趣の搶先報導
    • Raspberry Pi
    • 手機相關
    • 推薦軟體
    • 新鮮有趣
    • 生活資訊
  • 想破腦袋の程式技巧
    • Oracle EBS
    • Database
    • Excel, VBA
    • php
    • JavaScript, VBScript
    • VS.NET
    • Others
    • Windows
    • SAP
  • 撩動心弦の短文小品
  • 聚沙成塔の理財守則
  • 不可不知の職場實錄
  • 剎那永恆の生活翦影

近期文章

  • 受保護的內容: 如何透過Personalize功能呼叫另一form來回傳值
  • Win10 / 8 / 7 無法安裝 SSD
  • 受保護的內容: 樹梅派+遠端連線
  • EBS R12 取得客戶的phone, email, URL資料
  • 受保護的內容: 管控Workflow Administrator Role

友站

  • Masaya396's 協奏曲
  • 老塗的咁仔店

其他操作

  • 登入
  • 訂閱網站內容的資訊提供
  • 訂閱留言的資訊提供
  • WordPress.org 台灣繁體中文

Tag Cloud

你目前使用的瀏覽器不支援 HTML5 的 CANVAS 標籤。

  • php
  • HTC
  • VB6
  • EBS 11.5.10
  • WinXP
  • VB.Net
  • LDAP
  • EBS 12.1.3
  • CSS
  • excel
  • VBA
  • javascript
  • Oracle DB
  • Oracle EBS
  • Win7
  • 感情
  • SQL
  • MySql
  • 管理
  • SAP
© 2025 有害部落格同好會 - Powered by SimplyNews