【问题标题】:class selectors on javascript generated content [duplicate]javascript生成内容上的类选择器[重复]
【发布时间】:2014-12-19 03:53:01
【问题描述】:

这是一个重复的问题,乔治的上一个问题的链接有我的答案

我遇到了选择器无法处理动态生成的 javascript 内容的问题。

初始工作正常。一旦 for 循环生成更多 div,即使它具有相同的类,“鼠标悬停”css 样式也将不适用。

生成 div 的代码:

    for (x; x < y; x++) {
        output = output + '<div class="over">'+
            'But not for these generated divs'+ 
            '</div>';
    }
   $("#content").html(output);

使用“over”类设置 div 样式的代码:

$(".over").hover(function () {
    $(this).addClass("styling");
});


$(".over").mouseout(function () {
    $(this).removeClass("styling");
});

http://jsfiddle.net/kjhansen/1e08ypms/28/

【问题讨论】:

  • 我猜你的意思是 JavaScript,而不是 Java。
  • 对不起,我的粗鲁,但是当这个问题每天被问 100 次时,这个问题是如何获得 3 次赞成的?

标签: javascript jquery html css


【解决方案1】:

使用 jQuery on() 试试这个:

$(document).on('mouseover','.over',function () {
    $(this).addClass("styling");
});


$(document).on('mouseout','.over',function () {
    $(this).removeClass("styling");
});

FIDDLE EXAMPLE

【讨论】:

  • $(document).on('mouseover mouseout','.over',function () { $(this).toggleClass("styling"); });
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-01-22
  • 1970-01-01
  • 2014-08-14
  • 2016-09-25
  • 1970-01-01
  • 1970-01-01
  • 2013-11-23
相关资源
最近更新 更多