我是JQuery的新手,我對此有一個非常基本的問題。用JQuery修改某個ID的HTML元素
我有兩個html表格元素,一個id =「needColor」,另一個沒有id。
我想成立一個甚至行的背景色上表使用id =「needColor」,而不是在桌子上沒有任何標識。
我該如何使用JQuery來做到這一點?
當前代碼如下:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"</script>
<script>
$(document).ready(function(){
//tried to add something like these, but I am not sure how it must be written
// this makes all the even row in "all" table to be yellow
$("tr:even").css("background-color", "yellow");
// this will give the whole yellow color of table with id = needColor
$("table[id='needColor']").css("background-color", "yellow"); // do not use ("#needColor") as this will affect other elements than "table"
});
</script>
感謝。
我在您的代碼中看到一條評論指出:*不要使用(「#needColor」),因爲這會影響除「table」之外的其他元素*。不是這種情況。 ID始終是唯一的 - 您不應該給兩個或更多元素使用相同的ID。 –
另外,你也可以用css做同樣的事情'#needColor tr:even/tr:odd {/ *你的風格* /}' –
該操作令人困惑...首先你想使用'id'因爲你不用它將適用於所有表,然後第二你不想使用'ID',因爲它只適用於一個表?這是哪個? – guradio