JQuery函数问题:未捕获的ReferenceError [英] Problem with JQuery function: Uncaught ReferenceError
问题描述
我编写了一个函数,它从一个div返回一个元素数组,每个元素都有一个属性数组(ID,位置,类型(图像或文本)和文本或图像资源)。我认为主要的问题是,我在运行时从ID - $(#Counter).text()for循环中生成一个选择器,而不是我在那里写的变量 selector.toString( ),但它通常给我一个位置,但它没有给出类 - 而不是某种类型,有写的undefined。在这个if条件下,它会抛出这个错误:未捕获的ReferenceError:无效的左手边
if ($(selector.toString())。children()。first()。css( class)= img){
如何修复此错误或如何修复整个功能以执行我想要的操作?
这是整个功能评论:
function getAllElements(){
var elems = new Array (); // 为divplatno中的所有元素创建新数组。
for ( var i = parseInt ( 0); i< parseInt ($(< span class =code-string> #Counter)。text()); i ++){
var selector = # + i; // 从实际i创建一个选择器,代表ID。
var elem = new Array (); // 为元素的所有属性创建一个数组
elem [ 0 ] = i; // 首先是ID
elem [ 1 ] = $(selector.toString())。position(); // second is position
elem [ 2 ] = $(selector.toString())。children()。first()。css( 类跨度>); // 第三个是课程(文字或图片)
if ($(selector.toString())。children()。first()。css( class)= img){
elem [< span class =code-digit> 3 ] = $(selector.toString())。children()。first()。css( src); // 第四个是图像源
}
else {
elem [ 3 ] = $(selector.toString())。children()。first()。text ; // 或第四个是元素的文本
}
elems [i] = elem; // 向所有元素的数组添加元素
}
返回 elems; // 返回所有元素
}
非常感谢您提供良好的解决方案!
Pepin z Hane
(#Counter).text()for for循环而不是选择器我在那里写了变量 selector.toString(),但它给了我一个正常的位置,但是它没有给出类 - 而不是某种类型,有写的undefined。在这个if条件下,它会抛出这个错误:未捕获的ReferenceError:无效的左手边
if (
(selector.toString())。children()。first()。css( class)= img){
如何修复此错误或如何修复整个函数以执行我想要的操作?
以下是评论的整个函数:
function getAllElements(){
var elems = new Array (); // 为divplatno中的所有元素创建新数组。
for ( var i = parseInt ( 0); i< parseInt (
( #Counter)。text()); i ++) {
var selector = # + i; // 从实际i创建一个选择器,代表ID。
var elem = new Array (); // 为元素的所有属性创建一个数组
elem [ 0 ] = i; // 首先是ID
elem [ 1 ] =
Hi,
I have written a function that returns an Array of elements from one div and each element has an Array of attributes (ID, position, type(image or text) and text or image source). I think the main problem is, that I am generating a selector in the runtime from the ID - $(#Counter).text() in for cycle and instead of a selector I have written there the variable selector.toString(), but it gives me for example a position normally, but it doesn''t give the class - instead of some type, there is written undefined. And in this if condition it throws this error: Uncaught ReferenceError: Invalid left-hand side in assigment
if ($(selector.toString()).children().first().css("class") = "img") {
How to fix this error or how fix the whole function to do what I want?
Here is the whole function commented:
function getAllElements() {
var elems = new Array(); //creates new Array for all elements in div "platno".
for (var i = parseInt("0"); i < parseInt($("#Counter").text()); i++) {
var selector = "#" + i; //creates a selector from actual i, which represents ID.
var elem = new Array(); // creates an Array for all attributes of an element
elem[0] = i; //first is ID
elem[1] = $(selector.toString()).position(); //second is position
elem[2] = $(selector.toString()).children().first().css("class"); //third is class (text or image)
if ($(selector.toString()).children().first().css("class") = "img") {
elem[3] = $(selector.toString()).children().first().css("src"); //fourth is image source
}
else {
elem[3] = $(selector.toString()).children().first().text; //or fourth is text of an element
}
elems[i] = elem; // adds an element to Array of all elements
}
return elems; //returns all elements
}
Thanks a lot for good solutions!
Pepin z Hane
(#Counter).text() in for cycle and instead of a selector I have written there the variable selector.toString(), but it gives me for example a position normally, but it doesn''t give the class - instead of some type, there is written undefined. And in this if condition it throws this error: Uncaught ReferenceError: Invalid left-hand side in assigment
if (
(selector.toString()).children().first().css("class") = "img") {
How to fix this error or how fix the whole function to do what I want?
Here is the whole function commented:
function getAllElements() { var elems = new Array(); //creates new Array for all elements in div "platno". for (var i = parseInt("0"); i < parseInt(
("#Counter").text()); i++) { var selector = "#" + i; //creates a selector from actual i, which represents ID. var elem = new Array(); // creates an Array for all attributes of an element elem[0] = i; //first is ID elem[1] =
这篇关于JQuery函数问题:未捕获的ReferenceError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!