javascript - 设置 TextNode 样式

标签 javascript dom textnode

我正在使用单选按钮制作一个简单的多项选择程序。 当学生回答错误时,我可以在她的选择后面打一个X,但是如何将X变成红色呢? 谢谢你的帮助, 杰拉德

<body> 

<input type="radio" name="Q1" value="1756-1819,Berlioz" 
             onclick = "radioProcessor(this)" />      &nbsp; 1756-1819

<script>
function radioProcessor(theRadio){
        theRadio.nextSibling.nodeValue = theRadio.nextSibling.nodeValue + " X";
}
</script>

</body>

最佳答案

比如说,谢谢安德烈,但这没有用。我认为是因为 TextNode 保存文本,而不是其他元素(如跨度)。不过,多亏了你的想法,这是可能的 -

<input type="radio" name="Q1" value="1756-1819,Berlioz" 
             onclick = "radioProcessor(this)" /> <span>&nbsp; 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!

关于javascript - 设置 TextNode 样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31439610/

相关文章:

javascript - 为什么我的简单 angularjs 不起作用?

javascript - InfoWindow 打开缓慢或根本不打开

javascript - DOM 元素布局更改事件

javascript - jQuery : Select previous sibling of text node

jquery - Dom 节点操作,如何删除包裹我的选择的标签?

javascript - 如何获取文档片段中的所有文本节点?

javascript - 从元素数组中获取类列表

javascript - 我正在尝试用 html 和 javascript 制作一个简单的 tic tac toe 游戏

java - 在 Matlab 中编写 XML : How to add reference to DTD?

javascript - 动画仅适用于第一次单击按钮