一个简单的js问题 <table> <tr> <td><input type="checkbox" name="checkbox1" id="checkbox1" />语文</td> <td><input type="checkbox" name="checkbox2" id="checkbox2" />数学</td> </tr></table>当选中checkbox时,将所在单元格的背景设置成淡黄色,字体设置为粗体! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 <table> <tr> <td><input type="checkbox" name="checkbox1" id="checkbox1" onclick="abc(this)"/>语文</td> <td><input type="checkbox" name="checkbox2" id="checkbox2" onclick="abc(this)"/>数学</td> </tr></table><script>function abc(obj){ if(obj.checked){ obj.parentElement.style.backgroundColor="yellow"; obj.parentElement.style.fontWeight="bold"; }else{ obj.parentElement.style.backgroundColor=""; obj.parentElement.style.fontWeight=""; }}</script> 能不能保持原来的代码不变用DOM来实现? <table> <tr> <td><input type="checkbox" name="checkbox1" id="checkbox1" onclick="abc(this)"/>语文</td> <td><input type="checkbox" name="checkbox2" id="checkbox2" onclick="abc(this)"/>数学</td> </tr></table><script>function abc(obj){ if(obj.checked){ obj.parentNode.style.backgroundColor="yellow"; obj.parentNode.style.fontWeight="bold"; }else{ obj.parentNode.style.backgroundColor=""; obj.parentNode.style.fontWeight=""; }}</script>DOM?这样么? <input type="checkbox" name="checkbox1" id="checkbox1" onclick="abc(this)"/>就是怎样不添加onclick事件来实现! 我的js很菜,就是问一下不添加onclick事件是否可以实现? 事件动态绑定么?window.onload=function(){var list = document.getElementsByTagName("input");for(var i=0; i<list.length; i++){ var obj = list[i]; if(obj.type="checkbox"){ obj.onclick=function(){ abc(event.srcElement); } }}} 如果不动态也行,将实例具体化。window.onload=function(){var checkbox1=document.getElementById("checkbox1");if (checkbox1.checked="checked"){checkbox1.parentNode.style.backgroundColor="yellow"; checkbox1.parentNode.style.fontWeight="bold";}}最好用10楼的方法。 IE不能运行本地javascript,为什么? 高手进,js拖动的问题! 关于SVG格式在IE上的显示 javascript如何定义类中的静态变量? 打开窗口问题 函數問題,想不通,為什么就是調用不了呢? 怎样才能把树中的一节点通过鼠标移动到另一节点下 javascript传值问题?请问传值长度的限定 asp和javascript相互传递变量 windows XP下自动关闭窗口的问题? “网客兼职”网站是骗人吗?高手分析下这个代码 js里InnerHTMl如何加入样式
<tr>
<td><input type="checkbox" name="checkbox1" id="checkbox1" onclick="abc(this)"/>语文</td>
<td><input type="checkbox" name="checkbox2" id="checkbox2" onclick="abc(this)"/>数学</td>
</tr>
</table><script>
function abc(obj){
if(obj.checked){
obj.parentElement.style.backgroundColor="yellow";
obj.parentElement.style.fontWeight="bold";
}else{
obj.parentElement.style.backgroundColor="";
obj.parentElement.style.fontWeight="";
}
}
</script>
<tr>
<td><input type="checkbox" name="checkbox1" id="checkbox1" onclick="abc(this)"/>语文</td>
<td><input type="checkbox" name="checkbox2" id="checkbox2" onclick="abc(this)"/>数学</td>
</tr>
</table><script>
function abc(obj){
if(obj.checked){
obj.parentNode.style.backgroundColor="yellow";
obj.parentNode.style.fontWeight="bold";
}else{
obj.parentNode.style.backgroundColor="";
obj.parentNode.style.fontWeight="";
}
}
</script>
DOM?这样么?
就是怎样不添加onclick事件来实现!
var list = document.getElementsByTagName("input");
for(var i=0; i<list.length; i++){
var obj = list[i];
if(obj.type="checkbox"){
obj.onclick=function(){
abc(event.srcElement);
}
}
}
}
将实例具体化。window.onload=function(){
var checkbox1=document.getElementById("checkbox1");if (checkbox1.checked="checked"){
checkbox1.parentNode.style.backgroundColor="yellow";
checkbox1.parentNode.style.fontWeight="bold";}
}
最好用10楼的方法。