HTML:
<li id="video_button"><a href="#"></a></li>
JS:
if(ajax_status.responseText == "ready") {
document.getElementById("video_button").innerHTML = "START";
document.getElementById("video_button").onclick = function() {send_cmd("ca 1");};
halted = 0;
}
else if(ajax_status.responseText == "video") {
document.getElementById("video_button").innerHTML = "STOP";
document.getElementById("video_button").onclick = function() {send_cmd("ca 0");};
}
当我通过javascript标记列表项时,它无法识别HTML代码上的<a href="#"> </a>
。这样,它从我的CSS代码中的其他地方接收类。
如何通过JS向其添加href和class?
谢谢!
答案 0 :(得分:1)
是吗?只是在给定a
的情况下添加href
标签?
if(ajax_status.responseText == "ready") {
document.getElementById("video_button").innerHTML = "<a href='start'>Start</a>";
document.getElementById("video_button").onclick = function() {send_cmd("ca 1");};
document.getElementById("video_button").classList.add("myClass"); // Add Class
halted = 0;
}
else if(ajax_status.responseText == "video") {
document.getElementById("video_button").innerHTML = "<a href='stop'>Stop</a>";
document.getElementById("video_button").onclick = function() {send_cmd("ca 0");};
document.getElementById("video_button").classList.remove("myClass"); // Remove Class
}
答案 1 :(得分:1)
我建议阅读有关Element.setAttribute()的信息。