首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > JavaScript >

jquery中的each()不执行,是什么缘故?

2014-01-19 
jquery中的each()不执行,是什么原因??我想为名称为“.inputWithImge”的每个元素都执行函数,现遇到情况是:“.

jquery中的each()不执行,是什么原因??
我想为名称为“.inputWithImge”的每个元素都执行函数,现遇到情况是:“.inputWithImge”元素如果是在当前页面是可以执行这个函数的,但如果“.inputWithImge”元素是在浮动层的情况下,是不执行这个函数的,相当于each()不执行,那现要如何来解决这个问题呢?(保证“.inputWithImge”元素在浮动层中是存在的),代码如下:

$(document).ready(function() { <!--input对象加小图标-->ready()在文档加载后激活函数
   $(".inputWithImge").each(function(){
   $(this).add($(this).next()).wrapAll('<SPAN 



    }
    strHtml += "</td></tr></table>"
    div.innerHTML = strHtml;
    //浮动层显示页面
    document.body.appendChild(div);
    
    var winDiv = document.getElementById("layer1");
    winDiv.style.left = (document.body.offsetWidth - 680) / 2;
    winDiv.style.top = 180;
    //调整弹出框离IE最上边的距离空隙
    winDiv.style.display = "block";
    //这里加上就好了呀。。。。。。。。。
   
    $(".inputWithImge").each(function(){
        $(this).add($(this).next()).wrapAll('<SPAN class="imageInputWrapper"></SPAN>');
    });
    
}


你要是有奇怪的需求,必须得写在外面,那就这样[因为看不到视觉效果,所以这样猜的,不妨试试],当鼠标进入layer1层的时候就会自动激活
1:1.9+版本必须使用下面的on()

$(function() {
    $(document).on('mouseenter','#layer1',function(){
        $(".inputWithImge",this).each(function(){
            if(!$(this).parent().hasClass('imageInputWrapper')){
                $(this).add($(this).next()).wrapAll('<SPAN class="imageInputWrapper"></SPAN>');
            }
        });
    });
});
 

2:老版本可以使用live()
$(function() {
    $('#layer1').live('mouseenter',function(){
        $(".inputWithImge",this).each(function(){
            if(!$(this).parent().hasClass('imageInputWrapper')){
                $(this).add($(this).next()).wrapAll('<SPAN class="imageInputWrapper"></SPAN>');
            }
        });
    })
});

热点排行