我如何为es6执行相同的功能? 我正在尝试将此文本添加到列表项
var names = [ "Jon", "Nick", "Bill", "Tom" ];
$('#names-list li').each(function (index) {
$(this).text(names[index]);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul id="names-list">
<li></li>
<li></li>
<li></li>
<li></li>
</ul>
答案 0 :(得分:6)
ES6似乎与你所要求的无关。
你的问题的要点似乎是“如何在没有jQuery的情况下实现这一目标?”。在这种情况下,等效的是使用if Reachability.shared.isConnectedToNetwork(){
if let crew = user!["crew"] as? [String:Any], let crewAva = crew["crew_avatar"] as? String {
let imageUrlString = crewAva
let imageUrl:URL = URL(string: imageUrlString)!
let imageData:NSData = try NSData(contentsOf: imageUrl)
let image = UIImage(data: imageData as Data)
DispatchQueue.main.async(execute: {
do{
self.avaImg.image = image
}
catch{
print("error")
}
})
}
}else{
print("Not reachable")
}
获取querySelectorAll()
元素,li
循环遍历它们,然后设置forEach()
,如下所示:
textContent
var names = ["Jon", "Nick", "Bill", "Tom"];
document.querySelectorAll('#names-list li').forEach(function(li, index) {
li.textContent = names[index];
});
答案 1 :(得分:0)
如果您正在寻找100%的ES6,请转到:
app.directive('sameAs', function() {
return {
restrict: 'A',
require: {
ngModelCtrl: 'ngModel'
},
scope: {
reference: '<sameAs'
},
bindToController: true,
controller: function($scope) {
var $ctrl = $scope.$ctrl;
//add the validator to the ngModelController
$ctrl.$onInit = function() {
function sameAsReference (modelValue, viewValue) {
if (!$ctrl.reference || !modelValue) { //nothing to compare
return true;
}
return modelValue === $ctrl.reference;
}
$ctrl.ngModelCtrl.$validators.sameas = sameAsReference;
};
//do the check at each change
$ctrl.$onChanges = function(changesObj) {
$ctrl.ngModelCtrl.$validate();
};
},
controllerAs: '$ctrl'
};
});
但如果给我任何编码风格,我就不会真的写这样的代码。