请教各位老师,jQuery 给动态添加的元素元素加点击事件的
jquery吧
全部回复
仅看楼主
level 3
雨臣2016 楼主
请教各位老师,jQuery 给动态添加的元素元素加点击事件的问题,就是$(this)当前高亮,连续点击都成高亮了。跪求解决……
代码如下:
<pre>
<!DOCTYPE HTML>
<html>
<head>
<meta name="viewport" charset="utf-8" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<title>jquery解析xml</title>
<script src="ITlink/jquery-3.6.0.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$.post('books.xml',function(data){
//查找所有的book节点
var x="";
$(data).find('book').each(function(i){
var id=$(this).attr('id');
var name=$(this).children('name').text();
var author=$(this).children('author').text();
var price=$(this).children('price').text();
x+="<tr><td>"+id+"</td><td> "+name+" </td><td>"+author+" </td><td>"+price+"</td></tr>";
});
$("table").append(x);
});
$("body").on("click", "#mytable tr", function () {
$(this).css("color","red");
});
});
</script>
<style type="text/css">
table{border-collapse:collapse;margin: 0 auto 15px auto;width:98%;background:#FFFBE3;color:#333;
border-bottom: 1px solid #666;border-left:1px solid #666;
}
table tr:first-child{background: #666;color:#fff;height:26px;}
td{font-size:8px;padding: 0;
border-top: 1px solid #666;border-right: 1px solid #666;
}
</style>
</head>
<body>
<table id='mytable'></table>
</body>
</html>
</pre>
2021年10月18日 13点10分 1
level 3
雨臣2016 楼主
2021年10月18日 13点10分 2
level 3
雨臣2016 楼主
自学真难
2021年10月18日 13点10分 3
level 3
雨臣2016 楼主
求教了
2021年10月18日 18点10分 4
level 13
你既然点击会变色,其他已经变色的你得改回原来的色
2022年01月05日 07点01分 5
level 1
$("body").on("click","tr",function(e){
$("body").find("tr").css("color","black")
$("this").css("color","red")
})
2022年04月28日 00点04分 6
level 1
mouseover 事件在鼠标移动到选取的元素及其子元素上时触发 。
mouseover 事件在鼠标移动到选取的元素及其子元素上时触发 $(".p_content").mouseenter(function(){ $(this).css("color","blue") })
mouseout 事件在鼠标离开任意一个子元素及选的元素时触发。
mouseleave 事件只在鼠标离开选取的的元素时触发。
$(".p_content").mouseleave(function(){ $(this).css("color","red") })
2022年04月28日 01点04分 7
css伪类也可以:hover
2022年04月28日 01点04分
1