Javascript从元素获取id,然后使用它
Javascript get id from element and then use it
你能帮我解决这个问题吗?我不能在其他函数中使用返回x值。我希望当我点击某个元素时,脚本加载被点击元素的ID然后用这个ID改变元素的颜色。对我的问题有更好的解决办法吗?(在纯JS中,不是在Jquery中)谢谢。
<p id="1">foo</p>
<p id="2">bar</p>
<p id="3">baz</p>
<script>
document.addEventListener('click', function(e) {
x=e.target.id;
return x
});
document.getElementById(x).onclick =
function(x) {
if (document.getElementById(x).style.backgroundColor !== 'yellow') {
document.getElementById(x).style.backgroundColor = 'yellow';
}
else {
document.getElementById(x).style.backgroundColor = 'red';
}
};
</script>
把你的代码改成下面的
<p id="1">foo</p>
<p id="2">bar</p>
<p id="3">baz</p>
document.addEventListener('click', function(e) {
x=e.target.id;
function() {
var bgColor = document.getElementById(x).style.backgroundColor;
if (bgColor !== 'yellow') {
bgColor = 'yellow';
}
else {
bgColor = 'red';
}
}
});
</script>
好了,我找到了解决问题的方法。解决方案是把我所有的脚本在一个函数,然后一切工作。我学习JS关于1坐骑,现在我已经做了一个简单的熄灯游戏。现在我需要一些功能,检查所有细胞的颜色和警报游戏结束,但我不能回答一个新的问题,因为问题是不投票好,我不知道为什么。
下面是我的代码示例: document.addEventListener('click', function(e) {
var x = e.target.id
var up = ((Math.floor(x.charAt(0))-1).toString()).concat(x.charAt(1));
var down = ((Math.floor(x.charAt(0))+1).toString()).concat(x.charAt(1));
var left = (x.charAt(0)).concat((Math.floor(x.charAt(1))-1).toString());
var right = (x.charAt(0)).concat((Math.floor(x.charAt(1))+1).toString());
if( document.getElementById(x).style.backgroundColor == ''){document.getElementById(x).style.backgroundColor = 'black';}
else{document.getElementById(x).style.backgroundColor ='';}
if(document.getElementById(up)!=null){
if( document.getElementById(up).style.backgroundColor == ''){document.getElementById(up).style.backgroundColor = 'black';}
else{document.getElementById(up).style.backgroundColor ='';}}
if(document.getElementById(down)!=null){
if( document.getElementById(down).style.backgroundColor == ''){document.getElementById(down).style.backgroundColor = 'black';}
else{document.getElementById(down).style.backgroundColor ='';}}
if(document.getElementById(left)!=null){
if( document.getElementById(left).style.backgroundColor == ''){document.getElementById(left).style.backgroundColor = 'black';}
else{document.getElementById(left).style.backgroundColor ='';}}
if(document.getElementById(right)!=null){
if( document.getElementById(right).style.backgroundColor == ''){document.getElementById(right).style.backgroundColor = 'black';}
else{document.getElementById(right).style.backgroundColor ='';}}
// var all = document.getElementsByTagName("TD");
// var i;
// for (i = 0; i < all.length; i++) {
// all[i].style.backgroundColor!=='yellow';
// alert('a')
// break}
})
td {
padding: 50px;
background-color: yellow;
<table>
<tr>
<td id='11'></td>
<td id='12'></td>
<td id='13'></td>
<td id='14'></td>
<td id='15'></td>
</tr>
<tr>
<td id='21'></td>
<td id='22'></td>
<td id='23'></td>
<td id='24'></td>
<td id='25'></td>
</tr>
<tr>
<td id='31'></td>
<td id='32'></td>
<td id='33'></td>
<td id='34'></td>
<td id='35'></td>
</tr>
<tr>
<td id='41'></td>
<td id='42'></td>
<td id='43'></td>
<td id='44'></td>
<td id='45'></td>
</tr>
<tr>
<td id='51'></td>
<td id='52'></td>
<td id='53'></td>
<td id='54'></td>
<td id='55'></td>
</tr>
</table>
相关文章:
- 使用Jquery收集数组中的行ID,然后为新表查询这些值
- 选择具有ID的父级的第一个子级以外的所有子级,然后应用操作
- 为什么我们被教导将document.getElementById(“id”)分配给一个变量,然后将该变量与属性一起使用
- 通过getElementById获取id,然后设置值
- 创建一个bookmarklet,可以检索文本框的所有最大长度,然后在表中打印id和最大长度
- 计算具有特定类的元素的数量,然后添加对它们进行编号的ID
- 如何将标签 ID 作为参数传递给 javascript 函数,然后根据需要获得不同标签的文本
- 使用 JS 捕获链接 ID,然后定向到链接
- 将文本框中用逗号分隔的员工ID拆分为数组,然后处理这些字符串
- 如何使用 javascript 在
中搜索字符串,然后添加到该 id - 检查类和id是否存在,然后更改h1,然后在时间限制后重定向页面
- 将textareinnerhtml附加到数组中,然后在数组中搜索id
- 文件ElementID('id').innerHTML=只闪烁一次,然后它'他走了
- 将ID添加到Google地图标记,然后定位它
- 当用户在输入id=“”中填充文本时,如何调用函数java脚本;value_ 1”;然后按键盘上的“Enter”
- jQuery html onclick存储锚点ID,然后从javascript数组中提取内容
- 如何根据文本查找HTML元素,然后分配ID
- jQuery通过attr更改ID,然后调用它
- ID解析,然后使用选择器
- Javascript循环形式的值先名称,然后元素id