我的JavaScript技能並不是最好的,但我最近一直在努力研究它們,並且我想要做的一件事是爲html選項標籤設置屬性。我在jfiddle上發佈了這個項目,讓它更容易看清楚。你可以看到here。我想我只是一行代碼或其他東西。使用Javascript設置選中的atrribute onClick
我將如何從選項2中刪除所選屬性併爲選項1創建選定屬性?換句話說,當我打開'另一個彈出'時,選擇選項1而不是選項2.
我建議您查看jsFiddle鏈接,但我也發佈了有關此問題的代碼。 用於更新鏈接的JavaScript:
function updateSelected(id, removeID, parentID) {
document.getElementById(parentID).style.display = 'none';
document.getElementById(id).setAttribute("selected","selected");
document.getElementById(removeID).removeAttribute("selected")
}
的HTML:
<a href="#box" rel="popup">Popup</a>
<div id="box" class="popup">
<a href="#another-box" rel="popup" onClick="updateSelected("1", "2", "box");">Another box</a>
</div>
<div id="another-box" class="popup">
<form>
<select>
<option id="1">option 1</option>
<option id="2" selected>option 2</option>
</select>
</form>
</div>
感謝您的幫助
內不要有數字ID,即使一些瀏覽器支持它,並用雙引號內的單引號。請參閱控制檯 – mplungjan
我必須,因爲我使用php循環動態生成它們 – Alex
@mplungjan嚴格來說,數字標識符是HTML5功能。 – Alnitak