我用单选按钮做了一个简单的多项选择程序。当学生回答错误时,我可以在她的选择后面加一个X,但是我如何使X变红呢?谢谢你的帮助,杰拉德
<body>
<input type="radio" name="Q1" value="1756-1819,Berlioz"
onclick = "radioProcessor(this)" /> 1756-1819
<script>
function radioProcessor(theRadio){
theRadio.nextSibling.nodeValue = theRadio.nextSibling.nodeValue + " X";
}
</script>
</body>发布于 2015-07-16 04:22:52
我说,谢谢Andree,但这不管用。我认为是因为TextNode包含文本,而不是其他元素(如跨度)。然而,多亏了你的想法,这是可能的-
<input type="radio" name="Q1" value="1756-1819,Berlioz"
onclick = "radioProcessor(this)" /> <span> 1756-1819</span>
<script>
function radioProcessor(theRadio){
console.log(theRadio.nextElementSibling); // prints [object Text]
theRadio.nextElementSibling.setAttribute("style","color:red");
theRadio.nextElementSibling.innerHTML =
theRadio.nextElementSibling.innerHTML + " X";
}
它更近,但我只想要红色的"X“:-)每次按下单选按钮时,它还会继续添加X!
发布于 2015-07-16 05:13:49
这段代码适用于我。即使在单选按钮上单击两次也是如此。
<input type="radio" name="Q1" value="1756-1819,Berlioz"
onclick = "radioProcessor(this)" /> <span id="value"> 1756-1819</span>
<script>
function radioProcessor(theRadio){
var xAdded = document.getElementsByClassName("incorrect").length;
if(!xAdded) {
var result = document.createElement('span');
result.setAttribute("class", "incorrect");
result.innerHTML = theRadio.nextSibling.nodeValue + " X";
var value = document.getElementById("value");
value.parentNode.insertBefore(result, value.nextSibling);
}
}
</script>https://stackoverflow.com/questions/31439610
复制相似问题