2017-06-02 75 views
1

我得到了這個錯誤。我無法找到它有什麼問題。我使用PHP和JavaScript。更改所有使用javascript的元素類名稱的樣式

下面我有我的代碼隱藏的iframe:

<iframe id="hidden_form_submitter" name="hidden_form_submitter" style="width:100%;height:1px;visibility:hidden"></iframe> 

我有這行代碼,動作按鈕,其中一個排的id將發送作爲URL參數:

<tr bgcolor="#d1ffcf" class="row-<?=$transaction['id']?>"> 
<td> 
    <a class="done-btn" data-confirm="Are you sure?" href="?action=change&status=done&transact_id=<?=$transaction['id']?>" target='hidden_form_submitter' title="Click if matched!"> Done</a> 
</td> 
</tr> 

Then lastly I have this code on top to update the row in my database:

<? 
if($_GET['action'] == 'change'){ 

    //update query here 

    echo "<script>window.parent.document.getElementsByClassName('row-{$get['id']}').style.backgroundColor='green';</script>"; 

    exit; 
} 

?> 

在我的服務器更新端沒有問題。 問題是我想改變所有相同類的bgcolor而不重新加載整個頁面的JavaScript。

更新:無法更改每個類的背景。例如,我有這個參數要發送。 ?action=change&status=done&transact_id=1608 所以所有具有類row-1608的元素都應該改變背景顏色或改變某種風格。

+0

只是嘗試刪除{}在JavaScript –

+0

試圖刪除它是在我的編輯器synthax錯誤。 –

+0

將其更改爲「'row - 」。$ get ['id']。「''給了我同樣的錯誤。 –

回答

1

嘗試修改此:

echo "<script>window.parent.document.getElementsByClassName('row-{$get['id']}').style.backgroundColor='green';'</script>"; 

要這樣:

echo "<script>"; 
echo "var allRows = window.parent.document.getElementsByClassName('row-{$get['id']}');"; 
echo "for (var i = 0; i < allRows.length; i++) {"; 
echo " allRows[i].style.backgroundColor = 'green';"; 
echo "}"; 
echo "</script>"; 

您需要遍歷allRows並設置backgroundColor每個元素。

(本example啓發)

+0

那麼,刪除額外的'''給我一個新的錯誤'不能設置屬性'backgroundColor'未定義' –

+0

@ c.k我想我發現你的問題是什麼,你能檢查我更新的答案嗎? –

+0

現在,它正在工作。謝謝! –

相關問題