gpt4 book ai didi

javascript - 如何使用 jQuery 选择文本作为变量?

转载 作者:行者123 更新时间:2023-11-28 15:16:43 25 4
gpt4 key购买 nike

我有一堆数组,例如:

var myArrayName  = [1, 2, 3, 4, 5, 6];
var myArrayName2 = [1, 2, 3, 4, 5, 6];
var myArrayName3 = [1, 2, 3, 4, 5, 6];

在我的 DOM 中,我有以下元素 data-name="myArrayName"data-push="7", data-name="myArrayName2"data-push="2" 等等

我想选择所有带有 data-name 的元素,然后将 data-push 值推送给它们,所以:

$("*[data-name]").each(function(){
var arName = $(this).data("name");
var toPush = $(this).data("push");

// how do I make the stuff below work?
// arName.push(toPush);
})

最佳答案

这将选择具有属性data-name的所有元素

var arrDataAttr = new Array();
var arrDataPush = new Array();
$(document).find("[data-attr]").each(function()
{
arrDataAttr.push($(this).attr("data-attr"));
arrDataPush.push($(this).attr("data-push"));

});

关于javascript - 如何使用 jQuery 选择文本作为变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33573110/

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