一个简单的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楼的方法。 js 如何删除数组元素 大家看看我的作品,提个意见吧 javascript如何获得span的id呢 js高手帮忙 散分了,大家发些比较牛逼的jquery源码吧 [新手提问]很简单但是又很奇怪的问题,详见里面,很是郁闷 如何让IE不自动记录登陆密码框的密码? 请教JS高手,如何在网页里操作Windows的日历控件啊? TextBox的onblur事件中隐藏自己引发的问题 急!寻求逐段向上滚动的公告的特效。 “网客兼职”网站是骗人吗?高手分析下这个代码 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楼的方法。