简体   繁体   English

Javascript Split Function和Jquery不能一起工作

[英]Javascript Split Function and Jquery not working together

The Get variables of the URL need to be parsed. 需要解析URL的Get变量。 I have made a jQuery object of document.location and then have used the attr function to get the search attribute to get all the variables. 我创建了一个document.location的jQuery对象,然后使用attr函数获取search属性来获取所有变量。 But when i use the split function on it and after that each() is used it gives error stating that the object does not have a method each . 但是当我在它上面使用split函数并且之后使用each()时,它会给出错误,指出对象没有每个方法。

TypeError: Object [object Array] has no method 'each'  

Code is : 代码是:

 $(document.location).attr('search').split('&').each()

I have also tried to use the search property in the first function but it does not allow it ie $(document.location.search) gives error. 我也尝试在第一个函数中使用search属性,但它不允许它,即$(document.location.search)给出错误。

I have also checked that data type of the what is returned by split function, the console says that it is an object, i am also confused that it should have been an array. 我还检查了split函数返回的数据类型,控制台说它是一个对象,我也很困惑它应该是一个数组。

PS : all the above code goes in document.ready function of jQuery PS:以上所有代码都在jQuery的document.ready函数中

Making a jQuery object from the document.location object is pointless, because it's not a DOM element. document.location对象创建jQuery对象毫无意义,因为它不是DOM元素。

Just get the search property from the object, and use the $.each method intead of .each as you are looping an array, not elements: 刚刚得到的search从对象属性,并使用$.each方法这一翻译.each当你被循环数组,而不是元素:

$.each(document.location.search.split('&'), function(){
  ...
});

Try this: 试试这个:

$.each($(document.location).attr('search').split('&'), function (index, value) {
    alert(index + ': ' + value);
});

jQuery .each() method is used to iterate over a jQuery object, executing a function for each matched element. jQuery .each()方法用于迭代jQuery对象,为每个匹配的元素执行一个函数。

But what you get from the $(document.location).attr('search').split('&') is a JavaScript array, which obviously has no method 'each': that is why you are getting the error. 但你从$(document.location).attr('search').split('&')得到的结果$(document.location).attr('search').split('&')是一个JavaScript数组,显然没有'each'方法:这就是你得到错误的原因。

To loop through an array in jQuery you need to use $.each() method like above. 要在jQuery中循环遍历数组,您需要使用上面的$.each()方法。

As far as I know, JS-arrays don't have a each()-function. 据我所知,JS数组没有each()函数。

Try 尝试

var search_arr = $(document.location).attr('search').split('&');
for (var s in search_arr) {
    console.log(search_arr[s];
}

instead. 代替。

jQuery's each() function is meant to be called on its own, directly from jQuery; jQuery的each()函数本身就是直接从jQuery调用的; javascript arrays don't have it as a function, because it wasn't built that way. javascript数组没有它作为一个函数,因为它不是这样构建的。

I think you're looking for this: 我想你正在寻找这个:

$.each($(document.location).attr('search').split('&'), function (index, value) {
    // stuff
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM