2015-06-09 41 views
0

我有一個HTA,其中必須更新innerHTML與類driveLetter的一組元素。在我這樣做之前,我顯然必須獲取這個類的所有元素的數組。嘗試獲取具有特定類的所有元素時出錯

我試過這樣做與JS,但是我通過錯誤告訴兩種方法不支持(測試IE9和IE11)。在HTML文件中使用這些函數是可行的,但這是一個HTA。

對象不支持屬性或方法 'getElementsByClassName方法'
對象不支持屬性或方法 'querySelectorAll'

-

var driveLetterInstances = document.getElementsByClassName("driveLetter"); 
var driveLetterInstances = document.querySelectorAll(".driveLetter"); 

通過上面的行中產生的錯誤我沒有特別的必須使用JS,並且可以開放使用VBS來執行這個功能,但我不知道如何開始使用(或者甚至是可能的)。

+0

getElementsByClassName is supported IE9 + https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementsByClassName – AmmarCSE

+0

您可以在小提琴中複製問題嗎? – AmmarCSE

+0

你可以使用'getElementByClassName'或'querySelectorAll'來顯示你更新'innerHTML'的嘗試,並且你是否使用for循環遍歷元素數組? – NewToJS

回答

2

要更換一組元素的innerHTML你總是可以只做爲這樣一行簡單的東西:

JQuery的解決方案1:

// Find all the elements with the class name ".driveLetter" and replaces with "new content" 
 
$(".driveLetter").html("new content");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 

 
<!-- All elements with the same class name to be replaced with new content --> 
 
<div class="driveLetter"> Hello </div> 
 
<div class="driveLetter"> Hello </div> 
 
<div class="driveLetter"> Hello </div> 
 
<div class="driveLetter"> Hello </div>

的JQuery解決方案2:

// Loop through the classname 
 
$('.driveLetter').each(function(key, value) { 
 
    value.innerHTML = "New Content"; 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 

 
<!-- All elements with the same class name to be replaced with new content --> 
 
<div class="driveLetter"> Hello </div> 
 
<div class="driveLetter"> Hello </div> 
 
<div class="driveLetter"> Hello </div> 
 
<div class="driveLetter"> Hello </div>

的JQuery可以從JQuery的網站稱它可以使用,也可以從當地獲得本地存儲

例從網上

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 

例獲得文件

querySelectorAll的
<script src="js/jquery.min.js"></script> 

說明()

我相信IE8只支持標準模式querySelectorAll()。 REF:Check this

選擇器API被定義爲選擇器API 規範的一部分,僅適用於IE8 標準模式下顯示的網頁。

Selectors API

機會是你沒有設置合適的DOCTYPE聲明;你將需要添加一個。

+0

這是jquery Chris,而不是javascript。 –

+0

@AlejandroTeixeiraMuñoz「我沒有具體使用JS」閱讀他的問題 –

+0

在你的答案中沒有提及jQuery,它沒有在問題中標記。如果你打算給一個沒有標記的問題提供一個jQuery解決方案,我會建議清楚地顯示這是使用jQuery和如何喜歡到圖書館。 – NewToJS

相關問題