如何优化这段代码,使其代码行数更少、更简洁?
How can this code be optimised, so that it has fewer lines of code and is more consise?
我想缩短这段代码。比如循环什么的?如果有人帮助我缩短代码以便我可以添加更多内容,那就太好了 nth-child。喜欢 nth-child:4, 5.
$(document).ready(function(){
var src=$(".gallery-grid-item:nth-child(1) img").data("src");
$.ajax({
url:src,
xhrFields:{
responseType:'blob'
},
success:function(data){
var url=window.URL||window.webkitURL;
$(".gallery-grid-item:nth-child(1) img").attr("src",url.createObjectURL(data));
}
});
});
$(document).ready(function(){
var src=$(".gallery-grid-item:nth-child(2) img").data("src");
$.ajax({
url:src,
xhrFields:{
responseType:'blob'
},
success:function(data){
var url=window.URL||window.webkitURL;
$(".gallery-grid-item:nth-child(2) img").attr("src",url.createObjectURL(data));
}
});
});
$(document).ready(function(){
var src=$(".gallery-grid-item:nth-child(3) img").data("src");
$.ajax({
url:src,
xhrFields:{
responseType:'blob'
},
success:function(data){
var url=window.URL||window.webkitURL;
$(".gallery-grid-item:nth-child(3) img").attr("src",url.createObjectURL(data));
}
});
});
试试这个:
<script>
$(document).ready(function(){
for (var i = 1; i <= 3; i++) {
var cls = ".gallery-grid-item:nth-child(" + i +") img";
var src=$(cls).data("src");
$.ajax({
url:src,
xhrFields:{
responseType:'blob'
},
success:function(data){
var url=window.URL||window.webkitURL;
$(cls).attr("src",url.createObjectURL(data));
}
});
}
});
</script>
<script>
$(document).ready(function(){
numberOfNthChilds = 5
for (let i = 1; i < numberOfNthChilds + 1; i++) {
var src=$(".gallery-grid-item:nth-child(" + i + ") img").data("src");
$.ajax({
url:src,
xhrFields:{
responseType:'blob'
},
success:function(data){
var url=window.URL||window.webkitURL;
$(".gallery-grid-item:nth-child(" + i + ") img").attr("src",url.createObjectURL(data));
}
});
}
});
</script>
我想缩短这段代码。比如循环什么的?如果有人帮助我缩短代码以便我可以添加更多内容,那就太好了 nth-child。喜欢 nth-child:4, 5.
$(document).ready(function(){
var src=$(".gallery-grid-item:nth-child(1) img").data("src");
$.ajax({
url:src,
xhrFields:{
responseType:'blob'
},
success:function(data){
var url=window.URL||window.webkitURL;
$(".gallery-grid-item:nth-child(1) img").attr("src",url.createObjectURL(data));
}
});
});
$(document).ready(function(){
var src=$(".gallery-grid-item:nth-child(2) img").data("src");
$.ajax({
url:src,
xhrFields:{
responseType:'blob'
},
success:function(data){
var url=window.URL||window.webkitURL;
$(".gallery-grid-item:nth-child(2) img").attr("src",url.createObjectURL(data));
}
});
});
$(document).ready(function(){
var src=$(".gallery-grid-item:nth-child(3) img").data("src");
$.ajax({
url:src,
xhrFields:{
responseType:'blob'
},
success:function(data){
var url=window.URL||window.webkitURL;
$(".gallery-grid-item:nth-child(3) img").attr("src",url.createObjectURL(data));
}
});
});
试试这个:
<script>
$(document).ready(function(){
for (var i = 1; i <= 3; i++) {
var cls = ".gallery-grid-item:nth-child(" + i +") img";
var src=$(cls).data("src");
$.ajax({
url:src,
xhrFields:{
responseType:'blob'
},
success:function(data){
var url=window.URL||window.webkitURL;
$(cls).attr("src",url.createObjectURL(data));
}
});
}
});
</script>
<script>
$(document).ready(function(){
numberOfNthChilds = 5
for (let i = 1; i < numberOfNthChilds + 1; i++) {
var src=$(".gallery-grid-item:nth-child(" + i + ") img").data("src");
$.ajax({
url:src,
xhrFields:{
responseType:'blob'
},
success:function(data){
var url=window.URL||window.webkitURL;
$(".gallery-grid-item:nth-child(" + i + ") img").attr("src",url.createObjectURL(data));
}
});
}
});
</script>