Ajax未捕获的SyntaxError:意外的令牌"for"表示 [英] Ajax Uncaught SyntaxError: Unexpected token "for"

查看:87
本文介绍了Ajax未捕获的SyntaxError:意外的令牌"for"表示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

var dropdownsInsurance = ["home", "claims", "contact"];
					

	$.when(					
			for (var i = 0; i < dropdownsInsurance.length; i++) {
					$.get(dropdownsInsurance[i] + ".svg", function(svg) {
					$("#" + dropdownsInsurance[i]).append(svg.documentElement);
				});
			}
	
	).then(init);
	
	function init() {
		alert("svgs have been loaded!");
	}

.navLinkContainer {
  margin-top: 10px;
  width: 25%;
}

svg {
	padding: 0;
	margin: 0;
    width: 100%;
	display:block;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!DOCTYPE html>
<html>
<head>
	<link href="style.css" rel="stylesheet"/>
</head>

<body>

	<div id="mainMenu" class="menu">
		<div class="navLinkContainer"><a id="home"></a></div>
		<div class="navLinkContainer"><a id="claims"></a></div>
		<div class="navLinkContainer"><a id="contact"></a></div>
	</div>

</div>

</body>
</html>

我不确定在jquery ajax调用时如何在$.内部执行for循环.它不期望"for".这是一个插入链接,允许将外部svg拉入内部. https://plnkr.co/edit/HEcvX7N7Ih29tCNPatw1?p=preview 无论如何,同样的问题.我该怎么做呢?

I am not sure how to do a for loop inside a $.when jquery ajax call. It is not expecting the "for" . Here is a plunker link that allows the external svgs to be pulled inside. https://plnkr.co/edit/HEcvX7N7Ih29tCNPatw1?p=preview Anyway, same problem. How do I do this?

推荐答案

我猜想您想在所有$.get调用完成后运行init函数.为此,您需要将从$.get返回的每个promise作为参数传递给$.when.另外,您不能只将for循环粘贴在这样的函数调用中并期望它能正常工作.

I'm guessing you want your init function to run after all the $.get calls are done. To do this you need to pass each promise returned from $.get to $.when as a parameter. Also, you can't just stick a for loop inside a function call like that and expect it to work.

您需要构建一个承诺数组,然后使用.apply()将其发送到$.when.

You need to build an array of promises, then use .apply() to send them to $.when.

var dropdownsInsurance = ["home", "claims", "contact"];

// Array to hold promises from `$.get` calls
var svg_promises = [];

function init(){
    alert("svgs have been loaded!");
}

// Closure to capture each value of `i`.
// Otherwise, `i` will be the same value
// at the end of the loop, because the callbacks
// will run after the `for` is done and will use
// that value of `i`.
function svg_callback(i){
    return function(svg){
        $("#" + dropdownsInsurance[i]).append(svg.documentElement);
    };
}

for(var i = 0; i < dropdownsInsurance.length; i++){
    // Add each promise to the array
    svg_promises.push($.get(dropdownsInsurance[i] + ".svg", svg_callback(i)));
}

// Call `$.when` with all the promises
$.when.apply($, svg_promises).then(init);

DOCS:

Function.prototype.apply: https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Function/apply

$.when: http://api.jquery.com/jQuery.when/

这篇关于Ajax未捕获的SyntaxError:意外的令牌"for"表示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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