jquery - 是否可以使用 'div id' 作为数组名称?

标签 jquery css arrays wordpress frameworks

请查看此 jsfiddle jsfiddle.net/rflfn/uS4jd/

这是另一个尝试jsfiddle.net/rflfn/T3ZT6/

我正在使用 SMOF对于开发者 Wordpress 主题,我需要创建一个函数来在单击链接时更改某些值,但是当我创建名称为 div 的数组时,该数组返回空值...

HTML:

<a class="button" id="settext1">Some Link</a>
<br />

<a class="button" id="settext2">Another Link</a>
<br />

<a class="button" id="settext3">Link 3</a>
<br />

jQuery:

$(document).ready(function(){
    // var col_settext1 = new Array(); // <-- I need make this array with name of DIV cliked
    col_settext1['field_id1']='#FF0000';
    col_settext1['field_id2']='#00FFFF';

    // var txt_settext1 = new Array(); // <-- I need make this array with name of DIV cliked
    txt_settext1['field_id3']='Some Text Here';
    txt_settext1['field_id4']='Another Text Here';

    // var txt_settext2 = new Array(); // <-- I need make this array with name of DIV cliked
    txt_settext2['field_id5']='Some Text Here';

    // var col_settext2 = new Array(); // <-- I need make this array with name of DIV cliked
    col_settext2['field_id6']='Another Text Here';

    // var chk_settext2 = new Array(); // <-- I need make this array with name of DIV cliked
    chk_settext2['field_id7']="checked";
});

$('.button').click(function(){
    $myclass = this.id;

    $col = 'col_' + $myclass;
    $txt = 'txt_' + $myclass;
    $chk = 'chk_' + $myclass;

    // Based I clicked on the link 'settext1', Here I have this:
    // col_settext1
    // txt_settext1
    // chk_settext1

    // THE PROBLEM ARE HERE!
    $col = new Array(); // <--- Here I use name of DIV as Array, but the value is lost...
    $txt = new Array();
    $chk = new Array();

    // Test...
    alert($col); // <--- Here no have any value :(
    alert($col[1]); // <--- Here no have any value :(

    for (id in $col) { // 'id' is value of array --> col_settext1['field_id1']='#FF0000';
        // do function based on array values...
        // just example:
        alert(id);
    }
    for (id in $txt) { // 'id' is value of array --> txt_settext1['field_id1']='#FF0000';
        // do function based on array values...
    }
    for (id in $chk) { // 'id' is value of array --> chk_settext1['field_id1']='#FF0000';
        // do function based on array values...
    }

});

是否可以使用 div 的名称作为数组名称? 欢迎任何解决此问题的建议或任何其他方法。

最佳答案

如果我正确理解你的问题(即你想根据某些参数创建动态变量名),你应该使用下面的代码而不是“$col = new Array();”

window[$col] = new Array();
window[$txt] = new Array();

等..

关于jquery - 是否可以使用 'div id' 作为数组名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24003496/

相关文章:

jquery - 使用选择框滚动到页面上的某个位置

javascript - 悬停、焦点操作时的 JavaScript

jquery - 捕获用户在网站上的所有鼠标点击

css - IE 问题 - 图像周围不需要的边距

php - 引用 PHP 数组中的元素

javascript - 上传后未显示 Ajax 数据

css - 如果在使用 .SCSS/.SASS 文件时手动更新 .CSS 文件会发生什么情况?

javascript - slider 未出现

c - 将文件中的多行读入结构体数组 c

Java - 在值的第一个实例处拆分数组