与.live,.mouseover和.replaceWith / .html的jquery问题

时间:2011-05-12 14:34:03

标签: jquery

$("body *").live('mouseover', function() {
    var currentId = $(this).attr('id');
    var html = "<div id='perfect' style='font-size:10px;'><div id='pos1'><br>ID: " +currentId+ " <br>Klasse: " +currentClass+ " </div><div id='pos' style='width:300px'></div></div>";
    $("#perfect4").replaceWith(html);
});

不起作用 为什么

2 个答案:

答案 0 :(得分:0)

使用选择器“body *”,你的目标是身体的所有孩子。我想你会尝试这个(删除perfect4):

$("body").bind('mouseover', function(e) {
    var domTarget = e.target; // e.target grabs the node that triggered the event.
    var currentId = domTarget.id;
    var sHtml = "<div id='perfect' style='font-size:10px;'><div id='pos1'><br>ID: " +currentId+ " <br>Klasse: " +currentClass+ " </div><div id='pos' style='width:300px'></div></div>";
    var jPerfect = $("#perfect4");
    jPerfect.after(sHtml );
    jPerfect.remove();
});

在perfect4中替换innerHTML:

$("body").bind('mouseover', function(e) {
    var domTarget = e.target; // e.target grabs the node that triggered the event.
    var currentId = domTarget.id;
    var sHtml = "<div id='perfect' style='font-size:10px;'><div id='pos1'><br>ID: " +currentId+ " <br>Klasse: " +currentClass+ " </div><div id='pos' style='width:300px'></div></div>";
    $("#perfect4").html(sHtml);
});

答案 1 :(得分:-1)

你可以用这个:

$("#perfect4").text(html);

$("#perfect4").val(html);

$("#perfect4").html(html);