我尚未解决的与“悬停时获取元素ID(或悬停鼠标)"相关的问题 [英] An issue I have not resolved related to my 'Get element id on hover (or mouseover)' post

查看:73
本文介绍了我尚未解决的与“悬停时获取元素ID(或悬停鼠标)"相关的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上这是我的功能

function FillDivLeft(groups, side) {
 var cnt = 1;
 $.each(groups, function (index, groups) {
     $(side)
     .append(
         $(document.createElement('label')).attr({
             id: cnt + 'lbl',
             class: 'myClass'
         })
     );
     $('#' + cnt + 'lbl')
     .append(
         $(document.createElement('input')).attr({
             id: groups.GroupCode,
             type: 'checkbox',
             name: 'testGroup',
             class: 'leftClass'
         })
     );
     $('#' + groups.GroupCode).after
     (
         $(document.createElement('span')).text(groups.GroupName).attr({
             class: 'leftSpan'
         })
     );
     $('#' + cnt + 'lbl').after($(document.createElement('br')));
     cnt = cnt + 1;
 }); 
}

我尝试了各种尝试,尽管创建了新元素,分配了类等,但是页面上的所有jquery函数似乎均不起作用,例如:

I have tried all sorts and although the new elements are created, classes assigned etc. it appears that all jquery functions on the page do not work, such as:

$('.leftSpan').click(function () {
    $('#lblOutput').text(this.id);
});

即使父div上的悬停功能也不起作用.

Even a hover function on the parent div does not work.

我看不到可能的原因,因此不胜感激.

I can't see a possible cause so any help or pointers would be appreciated.

推荐答案

如果您使用的是jQuery 1.7+,则需要使用"on".您可以将"body"替换为元素的任何父div

You need to use 'on' if you are using jQuery 1.7+. You can replace 'body' with any parent div of the element

$('body').on('click','.leftSpan',(function () {
    $('#lblOutput').text(this.id);
});

仅使用.click函数需要在dom准备就绪时创建元素.它将不会将事件附加到动态创建的元素上.如果您使用的是jQuery 1.6及以下版本,则必须改用.live

Using just the .click function requires the elements to be created when the dom is ready. It will not attach the event to elements created dynamically. If you are using jQuery 1.6 and below you will have to use the .live instead

您可以在 http://api.jquery.com/on/

这篇关于我尚未解决的与“悬停时获取元素ID(或悬停鼠标)"相关的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆