[英]Using jquery to change background of an element created with Jquery
I am trying to display search results on my page for a list of people then allow the user to select one of those search results. 我试图在我的页面上显示搜索结果以列出人员,然后允许用户选择这些搜索结果之一。
I create a table of people using jquery, which each row having a class of .searchResult
and I want to highlight (or change background) of that row on hover but it doesn't seem to work the way I'm doing it. 我使用jquery创建了一个人表,每行都有一个
.searchResult
类,并且我想在悬停时突出显示(或更改背景)该行,但它似乎无法按照我的方式工作。
Is there an issue arising because they are rows that I have made after the page was loaded? 是否存在问题,因为它们是页面加载后我创建的行? Or is there an issue with changing the background color of a row?
还是更改行的背景色有问题?
JS JS
$(function(){
$('.searchResult').hover(function(){
$(this).css('background-color', '#ff0')
});
});
Here is a jsfiddle for how I'm trying to allow an action on the newly created rpws: http://jsfiddle.net/uxWwZ/ 这是一个jsfiddle,用于说明我如何尝试对新创建的rpws执行操作: http : //jsfiddle.net/uxWwZ/
You can do this with pure CSS: 您可以使用纯CSS做到这一点:
.searchResult:hover td {
background-color: #ff0;
}
http://jsfiddle.net/mblase75/6Pme2/ http://jsfiddle.net/mblase75/6Pme2/
I would suggest going with the CSS answer, but if you must stay with JQuery, you need to use delegation since they are being added after page load. 我建议使用CSS答案,但是如果您必须使用JQuery,则需要使用委托,因为它们是在页面加载之后添加的。
$(function(){
$(body).on('hover','.searchResult',function(){
$(this).css('background-color', '#ff0')
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.