gpt4 book ai didi

javascript - 在 Javascript 中返回数组

转载 作者:行者123 更新时间:2023-11-29 18:46:41 26 4
gpt4 key购买 nike

我正在使用 Dragsort 插件,我们想在数组中标记 ID 并打印它们。

我的 HTML 代码是:

<ul id="list1">
<li class="ss"><div>1</div></li>
<li><div>2</div></li>
<li><div>3</div></li>
<li><div>4</div></li>
<li><div>5</div></li>
<li><div>6</div></li>
<li><div>7</div></li>
<li><div>8</div></li>
<li><div>9</div></li>
</ul>
<p id="demo"></p>

我的 jQyery 代码是

$("#list1, #list2").dragsort({ dragSelector: "div", dragBetween: true, dragEnd: saveOrder, placeHolderTemplate: "<li class='placeHolder'><div></div></li>" });

function saveOrder() {
var data = $("#list1 li").map(function() { return $(this).children().html(); }).get();
$("input[name=list1SortOrder]").val(data.join("|"));
};

$( "#list1 li" ).each(function( index ) {
let items = [];
items = [ index + 1];
console.log(items.map(() => index));
$(this).attr("id",items);
});

$('#hamid').click(function () {
$( "#list1 li" ).each(function() {
its = [ $(this).attr('id')];
});
});

当我想返回 id <li> 时返回最后一个

最佳答案

你可能想换行

its = [ $(this).attr('id')];
//this will only create an instance of an array with one element.

将最后一个函数更改为:

$('#hamid').click(function () {
var ids = [];
$( "#list1 li" ).each(function() {
ids.push( $(this).attr('id') );
});
console.log(ids);
// this will print an array with all ids inside
// then you can return it or do whatever you want with it
});

关于javascript - 在 Javascript 中返回数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53297568/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com