【问题标题】:I have an array containing different div IDs how do i target specific IDs from within the aray javascript/jquery我有一个包含不同 div ID 的数组我如何从 aray javascript/jquery 中定位特定 ID
【发布时间】:2025-12-12 03:55:01
【问题描述】:

所以我的 script.js 文件中有一个数组。该数组包含大约 12 种不同的东西。我使用数组来存储 div 的 ID。因为我想动态加载这些 div。我已经完成了动态加载 div,但现在我想使用该数组在第一个 div 中加载内容(标题、图片等)。

let donorFeatureNames = [
    'SpawnVehicle',
    'RepairVehicle',
    'RocketVoltic',
    'MoreVehicle',
    'ChatColors',
    'Deagle',
    'M4',
    'Sniper',
    'CopFeature',
    'CrimFeature',
    'Changeskin',
    'Cash'
]

function loadFeatures () {
    for (i = 0; i < 12; i++) {
        $('#featureMenu').append('<div id=' + '"' + donorFeatureNames[i] + '"' + 'class="item notLoaded"></div>')

        $("'#" + donorFeatureNames[i] + "'").append(span class="title">' + $(this).data('donorfeature') + '</span>)
    }

我希望你明白我在问什么。因为我不太擅长解释事情。

【问题讨论】:

  • 您想在实例化期间或之后添加这些附加属性吗?
  • 我已经更新了帖子。主要关心的是如何让这部分工作: $("'#" +donorFeatureNames[i] + "'") 我可能知道该怎么做。
  • $(this).data('donorfeature') this 是什么?您是否在单击处理程序中调用此函数?另外$("'#" + donorFeatureNames[i] + "'") 应该只是$("#" + donorFeatureNames[i]) 而第二个append 引号全部关闭
  • 非常感谢!现在可以了。 $("'#" + donorFeatureNames[i] + "'") 都错了,我用你的评论重写了它。非常感谢您帮助我。

标签: javascript jquery


【解决方案1】:

不用数组,直接用jquery:

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index1001</title>
    <script src="~/Scripts/jquery-1.12.4.min.js"></script>
    <script type="text/javascript">
        //credit to https://*.com/questions/941206/jquery-add-image-inside-of-div-tag
        $(function () {
            $("#test1").append("Text");
            $('#test2').prepend("<img src='../../Images/w.JPG' />")
        })
    </script>
</head>
<body>
    <div id="test1" class="anArray"></div>
    <br/>
    <div id="test2" class="anArray"></div>
</body>
</html>

你甚至可以这样做来创建一个数组:$(".anArray").addClass("aColor");

【讨论】:

    【解决方案2】:

    黄教回答了我的问题。 $("'#" + donorFeatureNames[i] + "'") 不正确,但这是:$("#" + donorFeatureNames[i])

    这基本上是我问的。 非常感谢!

    【讨论】:

      最近更新 更多