更改所有使用javascript的元素类名称的样式

问题描述:

我得到了这个错误。我无法找到它有什么问题。我使用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']。“''给了我同样的错误。 –

尝试修改此:

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

现在,它正在工作。谢谢! –