jQuery .each css不是一个函数 [英] jQuery .each css is not a function

查看:320
本文介绍了jQuery .each css不是一个函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个jQuery对象与3个成员:

I have an jQuery object with 3 members:

var elements = $("#" + this.wrapperName + ">ul>li>a>img");
Object { 0: <img>, 1: <img>, 2: <img>, length: 3, prevObject: Object, context: HTMLDocument → JSUmstellung, selector: "#newsdesk>ul>li>a>img" }



我想改变每个CSS的属性, / p>

I want to change eachs CSS-Property, so I made this:

elements.each(function(index, element) {
    element.css({
        "width": elemWidth,
        "height": elemHeight,
        "top": elemTop,
        "left": elemLeft
    });
}

当我运行这个代码,我只得到以下错误:

When I run this code, I just get the following error:


TypeError:element.css不是函数

TypeError: element.css is not a function

我做错了什么?以另一种方式访问​​我的元素?

What am I doing wrong? Do I have to access my element in another way?

提前感谢!

推荐答案

问题是元素是一个DOM节点,无需访问jQuery方法,您需要使用 $(this) $(元素)

The problem is that element is a DOM node, without access to jQuery methods, what you need to use is $(this) or $(element):

elements.each(function(index, element) {
    $(this).css({
        "width": elemWidth,
        "height": elemHeight,
        "top": elemTop,
        "left": elemLeft
    });
}

或:

elements.each(function(index, element) {
    $(element).css({
        "width": elemWidth,
        "height": elemHeight,
        "top": elemTop,
        "left": elemLeft
    });
}

或者你可以使用 cssText (如果你真的节点):

Or you could, instead, use cssText (if you really want to work with DOM nodes):

elements.each(function(index, element) {
    element.style.cssText = 'width: ' + elemWidth + '; height: ' + elemHeight + ' top: ' + elemTop + '; left: ' + elemLeft;
}

请记住,每个(function(index,element){...}); element 这个完全相同(作为推论, $(this) $(element)相同。我不知道有什么好处,使用元素

Bear in mind that, within each(function (index, element){ ... }); element is exactly the same as this (and, as a corollary, $(this) is the same as $(element)). I'm not sure quite what the benefit is, of using the element at all.

这篇关于jQuery .each css不是一个函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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