addClass 使用 .live() 和 jQuery

发布于 08-10 05:20 字数 409 浏览 8 评论 0原文

我目前正在使用 .load() 函数将内容动态加载到容器 div 中。正在加载的内容是我想要斑马条纹的表格数据。斑马条纹在静态页面上很容易,但我不知道如何对加载到容器 div 中的新内容进行斑马条纹。

这是我正在尝试使用的代码:

$("table tbody tr:even").live("EVENT", function(){
  $(this).addClass("alt");
});

“EVENT”不应该是“click”“mouseover”,而是“onload”或类似的东西。有办法做到这一点吗?感谢您的建议!

〜贾里德

I am currently using the .load() function to load content into a container div dynamically. The content being loaded is table data which I'd like to zebra stripe. The zebra striping is easy on a static page, but I can't figure out how to zebra stripe the new content loaded into the container div.

Here's the code with which I'm trying to work:

$("table tbody tr:even").live("EVENT", function(){
  $(this).addClass("alt");
});

The "EVENT" shouldn't be "click", or "mouseover" but "onload" or something to that effect. Is there a way to do this? Thanks for your suggestions!

~Jared

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

千寻…2024-08-17 05:20:47

您应该只在 load() 的回调函数中运行斑马条纹代码。

$("#myDiv").load( "/somecontroller/someaction", { data: value }, function() {
    $("#myDiv").find( "table tbody tr:even" ).addClass( "alt" );
});

You should just run the zebra striping code in the callback function for the load().

$("#myDiv").load( "/somecontroller/someaction", { data: value }, function() {
    $("#myDiv").find( "table tbody tr:even" ).addClass( "alt" );
});
寄意2024-08-17 05:20:47

作为 tvanfosson 答案的扩展(在这种情况下这是你最好的选择), live() 函数目前仅支持某些事件。这是来自 http://api.jquery.com/ 的文档:

可能的事件值:click、dblclick、mousedown、mouseup、mousemove、mouseover、mouseout、keydown、keypress、keyup

目前不支持:blur、focus、mouseenter、mouseleave、change、submit

As an extension to tvanfosson's answer (Which is your best bet in this case), the live() function currently only supports some events. This is from the docs at http://api.jquery.com/:

Possible event values: click, dblclick, mousedown, mouseup, mousemove, mouseover, mouseout, keydown, keypress, keyup

Currently not supported: blur, focus, mouseenter, mouseleave, change, submit

巷子口的你2024-08-17 05:20:47

要动态地将数据添加到表中,您必须使用另一个代码。不是吗?
在该函数内,将数据加载到表后,将 css 类添加到行。
在该函数中使用以下方法,

$("table tbody tr:even").removeClass("alt").addClass("alt");

To add data to table dynamically, you have to use another code. Isn't it?
inside that function, after loading data to table, add css class to rows.
Using below method in that function,

$("table tbody tr:even").removeClass("alt").addClass("alt");
扛起拖把扫天下2024-08-17 05:20:47

我这样做是用 jquery 1.4.2

$("body").live("mousemove", function(){ $(".zebra tr:nth-child(even)").addClass(" 触发事件)替代"); });

I do it this way to trigger the event with jquery 1.4.2

$("body").live("mousemove", function(){ $(".zebra tr:nth-child(even)").addClass("alt"); });

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文